2012-02-16 10 views
1

1つのアプリケーションに2つのインスタンスのUITableViewがあります。問題は、各テーブルの表示方法を定義する方法がわかりません。ここでは、コードです:2つのTableViewを扱う方法

.H:

{ 
NSArray *array1; 
NSArray *array2; 
IBOutlet UITableView *table1; 
IBOutlet UITableView *table2; 
} 

.M:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array1 count]; 
} 

- (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] autorelease]; 
    } 

    cell.textLabel.text = [array1 objectAtIndex:indexPath.row]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
} 

このコードは、ちょうど一つのテーブルのために働いています。私はarray2で他のテーブルを設定する方法を知らない。アイデアはありますか?

+0

リストされたすべての委任されたメソッドには、tableViewパラメータがあります。テーブルビューがtable1かtable2かを比較して、それに応じて動作するかどうかを調べることができます。 – user523234

答えて

4

UITableViewのうちどれがdelegate/datasourceの方法で設定されているかを確認するだけです。試してみてください:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { 
    if(tableView == table1){ 
    return 1; // The number of sections in table1; 
    } 
else if(tableView == table2){ 
    return 1; // The number of sections in table2; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { 
    if(tableView == table1){ 
    return [array1 count]; 
    } 
    else if(tableView == table2){ 
    return [array2 count]; 
    } 
} 

- (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] autorelease]; 
    } 
    if(tableView == table1){ 
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];; 
    } 
    else if(tableView == table2){ 
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];; 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; { 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 
} 

希望がある!

+0

このようなオブジェクトをobjective-cで比較することはできません。 isEqual:(id)メソッドを使用する必要があります。このように比較すると、各ポインタのアドレスが比較されます。 –

+1

'isEqual:(id)'メソッドは実際にオブジェクトをチェックして別のオブジェクトと等しいかどうかを調べますが、 '=='は2つのオブジェクトのポインタを比較するだけです。しかしこの場合、 'IBOutlet UITablveView * table1'は' UITableView'のポインタへの参照を保持しています。この場合、ポインタのチェックは有効であるだけでなく速くなります! – msgambel

+0

うわー、このコード全体がうまくいきます!ありがとう、男! – Adri

0

これを行う通常の方法は、テーブルビューごとに個別のデータソース/デリゲートオブジェクトを作成することです。彼らは別々のクラスである必要はありません。同じクラスの2つのインスタンスにすることができます。

2

UITableViewDataSourceメソッドでは、ivarsとデリゲートを比較する必要があります。このような

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if([tableView isEqual:table1]) 
    { 
      return [array1 count]; 
    } 
    else if([tableView isEqual:table2]) 
    { 
      return [array2 count]; 
    } 
    else 
    { 
      return 0; 
    } 
} 

は、コールバック内のすべてのメソッドのためにこれを行います。

しかし、テーブルビューを1つしか持たず、いくつかのフラグに基づいて異なる内容をロードすることをお勧めします。この機能を有効にしてフラグを設定するには、[tableView reloadData]に電話する必要があります。次に、上記のコードをこのように変更します。if([flag isEqualToString:@"table1"]) { //code for table1 }

同じビューに2つのテーブルがある場合を除きます。それから、最初の方法はあなたがすべきことです。