tableViewCell的复用

今天犯了一个很愚蠢的错误,喷血中...
开始的时候代码是这么写的,注意separateView、progressLabel和customProgressView的位置。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString * string = @"string";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            _MyTableView.separatorColor = UITableViewCellSeparatorStyleNone;
            
        }
            //separateView
            UIView * separateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 20)];
            separateView.backgroundColor = RGBACOLOR(247, 247, 247, 1);
            [cell.contentView addSubview:separateView];
            //progressLabel
            UILabel * progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 79, 39, 21)];
            progressLabel.text = @"进度";
            progressLabel.textAlignment = NSTextAlignmentLeft;
            progressLabel.font = [UIFont systemFontOfSize:15];
            progressLabel.textColor = LABELTEXTCOLOR;
            [cell.contentView addSubview:progressLabel];
            //customProgressView
            CustomProgressView * customProgressView = [[CustomProgressView alloc]init];
            customProgressView.frame = CGRectMake(70, 45, SCREENWIDTH - 90, 90);
            customProgressView.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:customProgressView];
            _customProgressView = customProgressView;

        //任务已经完成
        if([_stateStr isEqualToString:@"show"])
        {
            _customProgressView.userInteractionEnabled = NO;
        }
        return cell;
}

我的customProgressView是一个自定义的带有滑块的的进度条,每次该cell滑出屏幕再滑进屏幕时,我的真机屏幕上就会多出一个customProgressView,画面彻底混乱了...

然后责怪单元格的复用机制没有起到作用...(汗啊...)

其实因为没有理解单元格的复用机制,代码放错了位置,正确的做法是把separateView、progressLabel和customProgressView的代码放在if(cell == nil){} 内。如下:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString * string = @"string";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            _MyTableView.separatorColor = UITableViewCellSeparatorStyleNone;
            //separateView
            UIView * separateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 20)];
            separateView.backgroundColor = RGBACOLOR(247, 247, 247, 1);
            [cell.contentView addSubview:separateView];
            //progressLabel
            UILabel * progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 79, 39, 21)];
            progressLabel.text = @"进度";
            progressLabel.textAlignment = NSTextAlignmentLeft;
            progressLabel.font = [UIFont systemFontOfSize:15];
            progressLabel.textColor = LABELTEXTCOLOR;
            [cell.contentView addSubview:progressLabel];
            //customProgressView
            CustomProgressView * customProgressView = [[CustomProgressView alloc]init];
            customProgressView.frame = CGRectMake(70, 45, SCREENWIDTH - 90, 90);
            customProgressView.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:customProgressView];
            _customProgressView = customProgressView;
        }
        //任务已经完成
        if([_stateStr isEqualToString:@"show"])
        {
            _customProgressView.userInteractionEnabled = NO;
        }
        return cell;
}

出错的图片稍后补上...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 坑:滑动tableView,cell会添加到复用的cell上 初步判断,是cell的复用有问题,看了看,有这么个方...
    海泉阅读 3,064评论 0 3
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 9,356评论 1 22
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,586评论 4 61
  • 今天是8月8日,早上起来运动时,还在纠结今天输出什么?是易效能1个月的检视,还是自己运动的心得,亦或是这两天聆听的...
    田园成长阅读 2,767评论 0 3
  • 开心, 周六我们一起去看了电影,“哆啦A梦:大雄的南极冰冰凉大冒险”,你和爸爸观影后的点评好有自己的特色。 开心:...
    蔡敏_Michelle阅读 1,441评论 0 1

友情链接更多精彩内容