为什么我的UISearchDisplayController访问非法的单元格/ indexPath?

托马斯

我有一个带有搜索栏的表格视图。最初,表视图显示所有(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调用

编辑:删除了关于混淆两个表视图的假设-一切似乎都在正确的位置,但是cellForRowAtIndexPath仍被称为第8行。还要注意,它始终是第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;
}
g

我认为你应该控制住tableView == self.searchDisplayController.searchResultsTableView此控件也应该在didSelectRowAtIndexPath:方法中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      if(tableView == self.searchDisplayController.searchResultsTableView)
      {
             UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" ];
             if (cell == nil) {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchResultCell"];
             }
      }
      else
      {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" ];
             if (cell == nil) {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
             }
      }
    return cell;
}

另一个想法:[tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" forIndexPath:indexPath]如果在情节提要或nib中初始化表格视图(在情节提要中设置单元格标识符),使用会更方便。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CollectionViewCell:通过 indexPath 访问单元格的数据?

如何知道单元格SWIFT的indexPath

UITableView-什么是indexPath?

@ [indexPath]是做什么的

根据indexPath.section和indexPath.row确定多维结构的单元格类型

如何获取单元格上uiview的indexPath-iOS

如何确定已轻按的开关的单元格的indexPath?

NSTableView获取具有单元格的indexPath

UICollectionView自动滚动到IndexPath的单元格

从单元格单击按钮时,获取UITableViewCell的indexPath

获取嵌入在UITableViewCell中的UITable的单元格的indexPath

如何获取位于UICollectionView中心的单元格的indexPath

Swift 5 CollectionView通过longPress单元格获取indexPath

快速获取collectionView中心的单元格的indexPath?

在indexpath的行单元格仅被调用一次

Swift:UICollectionView选择单元格indexPath问题

给定UITableViewCell的indexPath,加载该特定单元格

iOS:UICollectionview,如何通过indexPath获取单元格的contentoffset

在单元格中获取 indexPath.row

如何快速获得选定单元格的IndexPath

Xcode UICollectionView如何通过indexpath禁用某些单元格

关于表视图单元格indexpath.row

无法获得 collectionView 单元格的正确 indexPath

表视图单元格中的UIGestureRecognizer获取indexPath始终返回最后一个单元格的indexPath

快速表格视图单元格不允许我使用 indexPath.row

RxSwift访问indexPath.section

快速访问tableview indexPath ouside

访问用户的正确indexPath行

Swift 3:如何点击单元格内的按钮并传入该单元格的 indexPath?