7

で 2 つUITableViewのを使用してUIViewControllerいますが、両方のテーブルビューで行とセルを設定するにはどうすればよいですか? 2番目のテーブルビューを指定すると、セクション内の行数の重複宣言などが表示されます.

4

4 に答える 4

17

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

- (void)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _myTableViewOutlet1)
        return 10;
    else
        return 20;
}
于 2012-08-06T06:43:27.497 に答える
2

ご覧ください。

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

インターフェイスファイル内

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

実装ファイル内

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

このコードを使用します。希望が役立ちます

于 2012-08-06T07:06:16.727 に答える
2

すべてのUITableViewDelegateandUITableViewDatasourceメソッドは一度だけ実装されます。メソッドが呼び出されているテーブルビューを確認するだけです。

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

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

ここここを見て

このリンクには、問題の解決策もあります。

お役に立てれば

于 2012-08-06T06:45:44.487 に答える
1

可能です。ここで参照コードを見てください:http://github.com/vikingosegundo/my-programming-examples

このページも参照してください:1つのビューに2つのテーブルビュー

于 2012-08-06T07:18:07.720 に答える