通过加载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

转载于:https://www.cnblogs.com/xjf125/p/9497866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值