【问题标题】:UITableview remove the cell in visible regisionUITableview 删除可见区域中的单元格
【发布时间】:2023-03-15 09:40:01
【问题描述】:

我自定义了自己的 tableview,它包含几个部分,每个部分只包含一行。在 cellForRowAtIndexPath 中,我将 indexPath.section 存储为单元格的属性。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     KEShowcaseTableViewCell *cell = (KEShowcaseTableViewCell*)[tableView dequeueReusableCellWithIdentifier:sectionCellIdentifier forIndexPath:indexPath];

    if( cell == nil ){
        cell = [[KEShowcaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCellIdentifier];
    }


    cell.section = indexPath.section;

    cell.horizontalTableView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

但是,在我的 tableview 上上下滚动几次后,我发现 didEndDisplayingCell 中的单元格存储了与参数 indexPath.section 不同的 indexPath.section 值。

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
   if( cell.section != indexPath.section ){
       NSLog(@"something wrong!");
   }
}

我会发现我的 tableview 删除了仍在可见区域中的单元格。

我应该在哪里查看这个 tableview?

【问题讨论】:

  • 你能把代码贴在cellforrowatindexpath吗?
  • 你在哪里设置cell.section属性?

标签: ios objective-c iphone uitableview


【解决方案1】:

问题可能是,这个单元格已经在 tableView 中被重用了。因此,您实际看到的单元格不是消失的单元格,而是被重复使用并显示在表格中的单元格。

如果要获取单元格,必须手动调用cellForRowAtIndexPath

【讨论】:

  • 我同意该单元格可能已被重用...但是,didEndDisplayingCell 函数将让该单元格在我的表格视图中被删除...这将使表格视图在可见区域为空跨度>
  • “让这个单元格被删除”是什么意思?如果要删除单元格,则需要使用 indexPaths
  • 我的意思是单元格对象“A”可以被重用到另一个 indexPath(例如它首先在 section:0 中使用,并在 section:3 中重用)。但是,在 didEndDisplayingCell 函数(section:0)之后,section:3 上的视图将消失......它会让我的 tableview 显示错误的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多