通过加载Xib文件来创建UITableViewCell造成复用数据混乱问题方案

复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TradingAreaMyPraiseTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //解决xib复用数据混乱问题
    if (nil == cell) {
        cell= (TradingAreaMyPraiseTableViewCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"TradingAreaMyPraiseTableViewCell" owner:self options:nil]  lastObject];
    }else
  {
        while ([cell.contentView.subviews lastObject] != nil)
        {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
复制代码

每次创建cell之前,先进行比较一次,如果不存在再进行xib文件进行创建。

下面一种方法就比较暴力了,直接让其停止复用(数据量少时可以考虑用)

- (void)prepareForReuse {

    [super prepareForReuse];

    [_videoView reset];

}

 

UITableView在复用时造成cell分割线消失的问题解决方案

复制代码
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    //上分割线,
    //CGContextSetStrokeColorWithColor(context, COLORWHITE.CGColor);
    //CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
    CGColorRef color = [UIColor colorWithRed:236/255 green:236/255 blue:236/255 alpha:1].CGColor;
    //下分割线
    CGContextSetStrokeColorWithColor(context,color);
    CGContextStrokeRect(context,CGRectMake(10, rect.size.height-1, SCREEN_WIDTH-20,1));
}
复制代码

重写UITableViewCell的drawRect:方法


 

关于去除UITableViewCell复用机制的几种方法

https://blog.csdn.net/henry19890519/article/details/45693079

UITableView性能优化,超实用

https://blog.csdn.net/u011452278/article/details/60961350

posted @   Mr·Xu  阅读(1009)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示