我有一个带有搜索栏的表格视图。最初,表格视图显示所有 (10) 个项目。当我选择一个单元格时,导航控制器堆栈上会推送一个详细视图,然后我可以通过点击后退按钮返回到表格视图。只要我还没有输入搜索文本,表格视图就会显示所有项目,我可以无限期地在详细视图和表格视图之间来回切换。
当我在搜索栏中输入文本时,表格视图会使用 self.searchDisplayController.searchResultsTableView 正确更新(说它现在只显示 8 个项目),这意味着
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
被调用 8 次(第 0 节,第 0 到第 7 行)。美好的。
然后我可以点击一个单元格,查看详细视图,然后返回显示 8 个项目的表格视图。
如果我然后在表格视图中选择一个单元格仍然显示 8 个项目(与以前相同的单元格或不同的单元格,没关系),我再次看到详细视图,然后点击“返回”按钮,
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
为不存在的第 0 行第 8 行调用,因为它searchResultsTableView
仍然只显示搜索中的 8 个项目(第 0 到第 7 行)。
我真的不明白发生了什么:
- 搜索工作正常,
- 但
cellForRowAtIndexPath
用“错误”的 indexPath 调用
编辑:删除了关于混合两个表视图的假设 - 一切似乎都在正确的位置,但仍为第 8 行调用 cellForRowAtIndexPath。另请注意,它始终是第 8 行。
编辑 2:这是 cellForRowAtIndexPath。"Zeichen" 是一个 DAO,getZeichenForIndexPath:indexPath 从搜索结果中返回正确的实例。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" ];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchResultCell"];
}
Zeichen *zeichen = [self getZeichenForIndexPath:indexPath];
UIImage *img = [UIImage imageNamed:zeichen.filename];
cell.imageView.image = img;
cell.textLabel.text = [zeichen description];
return cell;
}