2012-08-06 11 views

答えて

17

これは、dataSource/delegateメソッドのパラメータがtableViewである理由です。その値に応じて、あなたは異なる数/細胞を返すことができます/ ...

- (void)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == _myTableViewOutlet1) 
     return 10; 
    else 
     return 20; 
} 
+0

、その後どのように与えられたために、セルのテキスト値とdidSelect行法 – user1567956

+2

をすべてのデータソース/デリゲートメソッド'tableView'パラメータを持っています。 'numberOfRowsInSection'だけでなく、' cellForRowAtIndexPath'や 'didSelectRowAtIndexPath'も含みます。 – Cyrille

2

あなたUITableViewDelegateUITableViewDatasource方法のすべては、一度だけ実行されます。メソッドが呼び出されているテーブルビューを確認するだけで済みます。

if (tableView == tblView1) { 
    //Implementation for first tableView 
} 
else { 
    //Implementation for second tableView 
} 

これはtableViewとしてテーブルビューのデリゲートとデータソースのすべてのメソッドで動作しますが、あなたの方法の全てに共通するパラメータである

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {} 
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {} 

herehere

This Linkはまたあなたのソリューションを持って見て問題。

希望します。

2

ご覧ください。

最初にインターフェイスビルダで2つのテーブルビューを作成し、次に2つのIBOutlet変数に接続し、両方のテーブルビューのデリゲートとデータソースを設定します。インタフェースで

は、このコードを使用し

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     if (tableView==tableView1) 
     { 
     return 1; 
     } 
     else 
     { 
     return 2; 
     } 
    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView==tableView1) 
    { 
     return 3; 
    } 
    else 
    { 
     if (section==0) 
      { 
      return 2; 
      } 
      else 
      { 
      return 3; 
      } 
    } 
    } 

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

    if (tableView==tableView1) 
     { 
      //cell for first table 
     } 
    else 
     { 
      //cell for second table 
      } 
    return cell; 
} 

実装ファイルで

-IBOutlet UITableView *tableView1; 
-IBOutlet UITableView *tableView2; 

を提出します。希望は

関連する問題