Iphone 使用静态单元格和另一个searchResultsTableView处理tableview

Iphone 使用静态单元格和另一个searchResultsTableView处理tableview,iphone,ios,objective-c,uitableview,ios5,Iphone,Ios,Objective C,Uitableview,Ios5,最初,我有一个主表视图控制器,其中包含使用故事板布局的静态单元格的表视图。随后,我将SearchDisplayController添加到此UITableViewController。在我的tableview数据源委托方法(如NumberOfSectionsTableView:和numberOfRowsInSection)中,我通过检查以下内容来区分我自己的tableview(带有静态单元格)和搜索显示控制器的searchResultsTableView: if (tableView == sel

最初,我有一个主表视图控制器,其中包含使用故事板布局的静态单元格的表视图。随后,我将SearchDisplayController添加到此UITableViewController。在我的tableview数据源委托方法(如NumberOfSectionsTableView:和numberOfRowsInSection)中,我通过检查以下内容来区分我自己的tableview(带有静态单元格)和搜索显示控制器的searchResultsTableView:

if (tableView == self.searchDisplayController.searchResultsTableView)
{
   // logic for searchResultsTableView;
}
else
{
  // logic for my main tableView
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
   // logic for searchResultsTableView;
}
else
{
   UITableViewCell *cell = [super tableView:tableView
                       cellForRowAtIndexPath:indexPath];
   return cell;

}
据我所知,这似乎是正确的方法。但是,当我尝试以下CellForRowatineXpath方法时,由于未捕获异常“NSInternalInconsistencyException”,消息终止应用程序时出现崩溃,原因是:“UITableView数据源必须从tableView返回单元格:CellForRowatineXpath:”

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        // Just want to use a default cell. There seems to be no good way of specifying a prototype cell for this in the storyboard.
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if ( cell == nil ) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
    else
    {
         // how to handle this case?
         return nil;
    }
}
以前,没有搜索显示控制器,我不必实现这个方法,因为我的单元格是静态的。我想我的问题是,我应该如何处理混合情况(在CellForRowatineXpath中,以及在同一故事板中指定两种单元格),其中我有一个带有动态单元格的搜索显示控制器tableview和一个带有静态单元格的tableview?我认为这种情况并不罕见

提前谢谢

编辑:

在调用else子句中的super方法时,正如第一个评论者所建议的那样,似乎可以修复崩溃,但我遇到了另一个问题,我觉得这是由于tableViewController作为静态tableview和非静态tableview(搜索显示结果表视图)的委托的固有问题造成的

新问题:我的静态tableview有两个静态单元格。当我用两行以上的行填充搜索结果tableview时,我会得到一个NSRangeException',原因:-[\uu NSArrayI objectAtIndex:]:索引2超出边界[0..1]

似乎searchResultsTableView以某种方式从我的主静态tableview中导出了行数(当我添加第三个静态单元格时,结果是一致的),即使委托方法:numberOfRowsInSection针对searchResultsTableView的情况被激发,并返回了正确的行数


让静态表视图与搜索显示表视图配合使用有什么解决方法吗?我正在考虑将我的主要静态表格视图转换为带有动态单元格的表格视图。。欢迎提出其他建议,谢谢

我最终的解决方法是将静态tableview转换为带有动态单元格的tableview。它不漂亮,但很管用。

我也遇到了这个问题。因此,我的决定是使用协议创建一些类,然后将其作为数据源分配给UISearchDisplayViewController。

您不需要将静态单元格更改为动态单元格。您可以执行以下操作:

if (tableView == self.searchDisplayController.searchResultsTableView)
{
   // logic for searchResultsTableView;
}
else
{
  // logic for my main tableView
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
   // logic for searchResultsTableView;
}
else
{
   UITableViewCell *cell = [super tableView:tableView
                       cellForRowAtIndexPath:indexPath];
   return cell;

}

这对您的问题很好。

我不确定,但您是否尝试过
返回[super tableView:tableView cellforrowatinexpath:indexPath]
?您的操作方法与tableView==self.searchDisplayController.searchResultsTableView返回true时的操作相同。您需要在IB中或以编程方式自己创建一些单元格,并根据您的需要创建和返回适当的单元格,可能是基于indexPath。谢谢我不知道为什么调用super会起作用,但是对于静态表视图,您根本不应该实现表视图数据源方法(它们的单元格应该通过IBOutlets填充)。我认为正确的方法是将else子句全部去掉。@rdelmar,你可能有道理。超级方法解决了最初的崩溃,但我遇到了新的问题,我怀疑这可能是因为tableviewcontroller是静态tableview和非静态tableview的委托。我已经用新的问题更新了我的问题。无论如何,cellForRowAtIndexPath必须返回UITableViewCell,所以我不知道如何去掉else子句。嗯,还有其他想法吗?