1

私のアプリでは、1つのテーブルビューで3つのテーブルビューを使用したいと思いViewControllerます。UITableViewDelegate問題は、メソッドを個別に使用するにはどうすればよいかということです。例えば; タグ付けすることで、cellForRowAtIndexPathメソッドを個別に使用できます。UITableViewしかし、各テーブルビューの使用方法numberOfRowsInSectionnumberOfSectionsInTableViewメソッドを異なる方法で使用することについてはわかりません。出来ますか?

4

8 に答える 8

4

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;
      }
于 2013-02-20T11:20:47.100 に答える
4

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
    }
}
于 2013-02-20T11:21:36.747 に答える
2

個別のデリゲート メソッドを使用しないでください。のような各デリゲート メソッドの代わりにcellForRowAtIndexPath、テーブルを次のように識別する必要があります。

if(tableview == TableView1) 
{ 

}
else if(tableview == TableView2)
{

}
else
{

}

等々 。これは、操作するすべてのテーブルが共通のデリゲート メソッドを持ち、テーブルの名前を指定するだけでよいため、正しいアプローチです。

于 2013-02-20T11:18:11.620 に答える
1

もちろん:

すべての tableview デリゲート関数は最初のパラメーターとして tableView を持っているため、3 つのテーブル ビューを追跡し、各デリゲート関数でデリゲート呼び出しがどのテーブル ビューに対して行われたかを確認するだけです。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == firstTableView) {
    ...
    }
    else if (tableView == secondTableView) {
    ...
    }
    else if (tableView == thirdTableView) {
    ...
    }
}
于 2013-02-20T11:18:44.907 に答える
1

また、3 つのテーブル ビュー コントローラー クラスを作成することもできます (すべてのテーブルビューのセル表示ロジックが少し複雑になる場合に備えて)。[self addChildViewController:(Your Controller Class)それらを次の行として追加し、[self.view addSubview:(Your Controller Class' view)]設定するフレームにビューを調整して次の行に追加します。

于 2013-02-20T11:29:28.113 に答える
1

このチュートリアルが役に立つかもしれません http://www.edumobile.org/iphone/ipad-development/a-tableview-based-menu-for-ipad/を参照してください

于 2013-02-20T12:01:57.113 に答える
0

すべてのデリゲート メソッドとデータソース メソッドで、最初のパラメーターは tableview オブジェクトへの参照です。したがって、いつでもテーブルビューを区別できます。

于 2013-02-20T11:19:19.263 に答える
0

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;
}

お役に立てば幸いです。

于 2013-02-20T11:29:15.083 に答える