• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableView reloadRowsAtIndexPaths : 上消失的 UITableViewCell

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:36:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一些从 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap