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

猜你喜欢

转载自www.cnblogs.com/xjf125/p/9497866.html
今日推荐