如何在iphone的单个视图控制器中使用两个以上的UITableView



我在一个UIViewController中使用两个UITableView,如何填充两个表视图中的行和单元格?当我给出第二个表视图时,它显示了部分等中行数的重复声明。

这就是dataSource/delete方法具有tableView参数的原因。根据其值,您可以返回不同的数字/单元格/。。。

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

您的所有UITableViewDelegateUITableViewDatasource方法将只实现一次。您只需要检查调用该方法的表视图。

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

看看这里和这里

此链接还提供了您的问题的解决方案。

希望这能帮助

请看一看。

首先在接口生成器中创建两个表视图,然后连接两个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;
 }

使用此代码。希望帮助

可以在这里查看参考代码:http://github.com/vikingosegundo/my-programming-examples

另请参阅本页:单视图上的2个表视图

最新更新