OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView reloadRowsAtIndexPaths : 上消失的 UITableViewCell [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:36
标题: ios - UITableView reloadRowsAtIndexPaths : 上消失的 UITableViewCell

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

我还注意到,当我调用 [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"rofileTableViewCell" owner:nil options:nil];
    ...
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    int cellId = /* some logic to get the correct cellID */
    NSString *CellIdentifier = [NSString stringWithFormat"rofileTableViewCell_%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];
    ...
}

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



Best Answer-推荐答案


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

关于ios - UITableView reloadRowsAtIndexPaths : 上消失的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18214008/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4