一个控制器中有两个表格视图

4

我正在尝试在一个视图中制作两个表格视图,但遇到了一些问题。我已经阅读了一些关于如何做到这一点的其他回答,但它们并没有完全帮助到我。

在我的.h文件中,我为两个视图创建了两个输出口,分别称为myFirstViewTextmySecondViewTex

因此,在我的.m文件中,对于- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

我想要能够在每个不同的控制器中打印出单独的值,但是我不确定,因为您只返回1个单元格?

到目前为止,我已经完成了以下工作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Rx";
static NSString *CellIdentifier2 = @"allergies";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2];
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (!cell2) {
    cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
if (tableView == self.myFirstTextView ) {
       cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
if (tableView == self.mySecondTextView) {
      cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
tableView = self.mySecondTextView;
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;

在我的第一个表格视图中,这会打印"I love Jazzy",而在第二个表格视图中没有任何内容被打印。为什么会出现这种情况,我该如何解决呢? 谢谢 :D

4个回答

7

此方法由所有将您的类实例设置为其数据源的tableView调用。

这意味着您需要检查是哪个tableView请求了某些单元格的编号。

因此,您的方法应该基本上像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == rxTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    else if (tableView == allergiesTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    return nil; //because you don't expect any other tableView to call this method
}

请注意,返回nil将导致程序崩溃... 最好返回一个空单元格或向用户警告无法恢复的错误,并要求他们重新启动应用程序。 - lnafziger
1
@lnafziger 你说得对,这会导致应用程序崩溃。我认为这是一件好事。你不希望任何你不知道的 TableViews 请求数据,而且你也无法提供它。尽早崩溃是好的,如果某些东西不能静态检查,就像在这种情况下一样。无论如何,这已经无关紧要了——那一行永远不应该到达哪里。 - fzwo
其实,在我看来,当你可以提供可控制的关闭时,崩溃是不好的。但是,当你在代码中包含它时,仍然值得向那些不知道的人指出。 :) - lnafziger

4

我的建议是为你的两个表视图都设置一个标记,然后在你的tableview数据源方法中检查你所在的表视图的标记。例如,在你的代码的某个地方,你可以这样做:

myFirstTableView.tag = 1;
mySecondTableView.tag = 2;

稍后当你在……
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.tag == 1) 
   cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];

   if (tableView.tag == 2) 
   cell.textLabel.text = @"BYE JAZZY";
 }

此外,如果您有两个不同大小的表格视图,则可以按照以下方式实现此目标:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    if (tableView.tag == 1)
         return 1;
    else if (tableView.tag == 2)
         return 2;
  }

1
问题1:
tableView = self.mySecondTextView;     
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;

你现在做的是始终返回cell2,并设置其文本为"I love jazzy :D",所以它将始终填充第一个表视图,因为在进入该方法之前你没有设置tableView=mySecondTextView。委托始终针对firstTableView
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

问题2:
if (tableView == self.myFirstTextView ) {
    cell.textLabel.text = @"HI JAZZY";
    //[RxDict objectAtIndex:indexPath.row];

    //for printing in first table view return cell here like

    return cell;
}
if (tableView == self.mySecondTextView) {
    cell.textLabel.text = @"BYE JAZZY";
    //[RxDict objectAtIndex:indexPath.row];

    //for printing in second tableview return cell two here

    return cell2;
}

在进入此函数之前,请确保您要定位的表格已设置。您可以在numberofsection方法中设置它,在视图didload方法中设置,或者从您想要显示或定位表格视图的其他地方设置(例如单击某个按钮)。但是您不能在此处设置。


1

我看到的一个不太常见的方法是使用NSObject子类作为每个表格的委托和数据源,而不是视图控制器。这样可以避免“if (tableView == someTable) …”代码的需要,并且可能使您的视图控制器和UITableView代码易于维护和阅读。

您可以为每个表格创建一个NSObject子类。这些子类将包含所需的UITableView数据源和委托方法的实现。然后,在视图控制器中实例化每个对象,并将每个对象连接到其相应的表格。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接