私のアプリでは、1つのテーブルビューで3つのテーブルビューを使用したいと思いViewController
ます。UITableViewDelegate
問題は、メソッドを個別に使用するにはどうすればよいかということです。例えば; タグ付けすることで、cellForRowAtIndexPath
メソッドを個別に使用できます。UITableView
しかし、各テーブルビューの使用方法numberOfRowsInSection
やnumberOfSectionsInTableView
メソッドを異なる方法で使用することについてはわかりません。出来ますか?
8 に答える
datasource メソッドと delegate メソッド内に Conditions があるすべてのテーブルに対して、dataSource メソッドと Delegate メソッドを 1 つだけ使用します。
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
{
if (tableView==tabl1) {
return [arr1 count];
}
if (tableView==tabl2) {
return [arr2 count];
}
if (tableView==tabl3) {
return [arr3 count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
}
if (tableView==tabl1) {
cell.textLabel.text = [arr1 objectAtIndex:indexPath.row];
}
if (tableView==tabl2) {
cell.textLabel.text = [arr2 objectAtIndex:indexPath.row];
}
if (tableView==tabl3) {
cell.textLabel.text = [arr3 objectAtIndex:indexPath.row];
}
return cell;
}
YourViewController.hで3 つUITableView
の変数を作成します。
YourViewController : UIViewController
{
UITableView* tableView1;
UITableView* tableView2;
UITableView* tableView3;
}
YourViewController.m:
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == tableView1)
{
//Your code
}
if (tableView == tableView2)
{
//Your code
}
if (tableView == tableView3)
{
//Your code
}
}
個別のデリゲート メソッドを使用しないでください。のような各デリゲート メソッドの代わりにcellForRowAtIndexPath
、テーブルを次のように識別する必要があります。
if(tableview == TableView1)
{
}
else if(tableview == TableView2)
{
}
else
{
}
等々 。これは、操作するすべてのテーブルが共通のデリゲート メソッドを持ち、テーブルの名前を指定するだけでよいため、正しいアプローチです。
もちろん:
すべての tableview デリゲート関数は最初のパラメーターとして tableView を持っているため、3 つのテーブル ビューを追跡し、各デリゲート関数でデリゲート呼び出しがどのテーブル ビューに対して行われたかを確認するだけです。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == firstTableView) {
...
}
else if (tableView == secondTableView) {
...
}
else if (tableView == thirdTableView) {
...
}
}
また、3 つのテーブル ビュー コントローラー クラスを作成することもできます (すべてのテーブルビューのセル表示ロジックが少し複雑になる場合に備えて)。[self addChildViewController:(Your Controller Class)
それらを次の行として追加し、[self.view addSubview:(Your Controller Class' view)]
設定するフレームにビューを調整して次の行に追加します。
このチュートリアルが役に立つかもしれません http://www.edumobile.org/iphone/ipad-development/a-tableview-based-menu-for-ipad/を参照してください
すべてのデリゲート メソッドとデータソース メソッドで、最初のパラメーターは tableview オブジェクトへの参照です。したがって、いつでもテーブルビューを区別できます。
のViewController.h
{
NSArray *arr1;
NSArray *arr2;
NSArray *arr3;
}
@property (nonatomic, retain) IBOutlet UITableView *tbl1;
@property (nonatomic, retain) IBOutlet UITableView *tbl2;
@property (nonatomic, retain) IBOutlet UITableView *tbl3;
XIB's
このプロパティをUITableView インスタンスに接続することを忘れないでください。
のViewController.m
@synthesize tbl1, tbl2, tbl3;
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (tableView == tbl1)
return [arr1 count];
if (tableView == tbl2)
return [arr2 count];
if (tableView == tbl3)
return [arr3 count];
return 0;
}
お役に立てば幸いです。