最初,我有一个主表视图控制器,其中包含使用情节提要布置的静态单元格的表视图。随后,我在这个 UITableViewController 中添加了一个 SearchDisplayController。在我的 tableview 数据源委托方法中,例如 numberOfSectionsInTableView: 和 numberOfRowsInSection,我通过检查以下内容来区分我自己的 tableview(带有静态单元格)和搜索显示控制器的 searchResultsTableView:
if (tableView == self.searchDisplayController.searchResultsTableView)
{
// logic for searchResultsTableView;
}
else
{
// logic for my main tableView
}
据我所知,这似乎是正确的方法。但是,当我为 cellForRowAtIndexPath 方法尝试以下操作时,由于未捕获的异常“NSInternalInconsistencyException”,我收到消息正在终止应用程序,原因是:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格:”
- (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;
}
}
以前,没有搜索显示控制器,我不必实现此方法,因为我的单元格是静态的。我想我的问题是,我应该如何处理混合案例(在 cellForRowAtIndexPath 中以及在同一个故事板中指定两种单元格),其中我有一个带有动态单元格的搜索显示控制器 tableview 和一个带有静态单元格的 tableview?我想这种情况并不少见。
提前致谢!
编辑:
虽然按照第一个评论者的建议在 else 子句中调用 super 方法似乎可以修复崩溃,但我遇到了另一个问题,我觉得这是由于 tableViewController 作为静态 tableview 和非-static 一(搜索显示结果表视图)。
新问题:我的静态表格视图有 2 个静态单元格。当我用超过 2 行填充我的搜索结果表视图时,我得到一个 NSRangeException',原因:-[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]。
似乎 searchResultsTableView 以某种方式从我的主静态 tableview 派生了行数(当我添加第三个静态单元格时结果是一致的),即使委托方法:numberOfRowsInSection 被触发为 searchResultsTableView 的情况,并返回正确的数字。
使静态表格视图与搜索显示表格视图一起使用的任何变通方法?我正在考虑将我的主要静态表格视图转换为带有动态单元格的表格视图。欢迎任何其他建议,谢谢!