【问题标题】:Disappearing UITableViewCell on UITableView reloadRowsAtIndexPaths:UITableView reloadRowsAtIndexPaths 上消失的 UITableViewCell:
【发布时间】:2013-08-13 16:06:55
【问题描述】:

我有一些从 XIB 文件加载的 UITableViewCell。一切都很好,直到我在单元格消失时调用 [UITableView reloadRowsAtIndexPaths:withRowAnimation:] 方法。当我调用 [UITableView reloadData] 加载所有内容时,当我在视图上下滚动单元格时,它也会重新出现。很奇怪。

我还注意到,当我调用 [UITableView reloadRowsAtIndexPaths:withRowAnimation:] 时,UITableView 不会尝试重用缓存的单元格,而是会尝试使用 cell = [tableViewCells objectAtIndex:cellId]; 获取一个新单元格;

这是我的代码:

- (void)viewDidLoad
{
    ...
    // the objects from the XIB files are loaded onto an NSArray instance variable at viewDidLoad
    tableViewCells = [[NSBundle mainBundle] loadNibNamed:@"ProfileTableViewCell" owner:nil options:nil];
    ...
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int cellId = /* some logic to get the correct cellID */
    NSString *CellIdentifier = [NSString stringWithFormat:@"ProfileTableViewCell_%d", cellId];
    ProfileTableViewCell *cell = (ProfileTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [tableViewCells objectAtIndex:cellId];
        // additional work to setup subviews for the cell
        [cell prepareSubviews];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
}

以防万一这是我在 [ProfileTableViewCell prepareSubviews] 中所做的一些事情

- (void)prepareSubviews
{
    ...
    [self.containerView.layer setCornerRadius:3];
    [self.containerView.layer setBorderColor:UIColorFromRGB(0xCDCDCD).CGColor];
    [self.containerView.layer setBorderWidth:2.0];
    [self.containerView.layer setMasksToBounds:YES];
    [self.containerView.layer setShouldRasterize:NO];
    [self.containerView.layer setRasterizationScale:[UIScreen mainScreen].scale];
    ...
}

提前感谢可以帮助我的好人。

【问题讨论】:

    标签: ios objective-c uitableview uikit


    【解决方案1】:

    我不确定这是你的问题,但你获取细胞的方式不是正常的方式。您应该在自己的 nib 中创建每种类型的单元格(具有唯一标识符),然后使用 registerNib:forCellReuseIdentifier: 注册 nibs。在 cellForRowAtIndexPath 中,只需根据索引路径将需要的单元格出列即可,不要在 if (cell == nil) 子句中放入任何内容,因为这样做不会调用它。

    【讨论】:

    • 同意。他的代码从 viewDidLoad 中的 nib 实例化单个单元格,但随后可能会为不同的行多次返回相同的单元格实例。这就是单元格消失的原因 - 表格视图将同一单元格从一行重新定位到另一行。
    • 实际上 ProfileTableViewCell.xib 包含 6 个具有不同重用 ID 的不同 UITableViewCells,我使用 cell = [tableViewCells objectAtIndex:cellId]; 调用适当的一个。其中 cellId 将是 XIB 中对象的索引号。我想我可以通过加载一个包含我需要加载 6 个不同 XIB 的所有单元格的单个 xib 来节省一些开销。既然我明白这是不标准的东西,我可能会尝试不同的方法。非标准逻辑总是有毛刺。
    • @JihoKang,最简单的方法是在故事板中完成。您可以在一个表格视图中创建所有 6 个单元格。
    【解决方案2】:

    当您只需要更改静态单元格的高度时,reloadRowsAtIndexPaths: 不起作用。

    试着只打电话

    [tableView beginUpdates];
    [tableView endUpdates];
    

    正如文档所说:

    您也可以使用此方法后跟 endUpdates 方法 在不重新加载单元格的情况下对行高的变化进行动画处理。

    【讨论】:

      猜你喜欢
      • 2013-11-04
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多