Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 446|回复: 0

关于UITableView的cell复用问题的3种解决方法

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 772 天

    [LV.10]以坛为家III

    2044

    主题

    2102

    帖子

    71万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    713294
    发表于 2021-7-11 17:47:45 | 显示全部楼层 |阅读模式

    最近在好多地方又遇到有人提tableview的复用问题,觉得还是说下自己的理解,希望能有帮助!

      之前就想写自己关于复用的想法,拖了这么久,又有人被困惑,所以就写了。

      事实上复用问题的本质是cell上面的控件的内容指针没有重指向、button事件重复添加等!

     比如:指针重指向:cell.textLabel.text = model.name;这个就是label上内容的指针重指向,所以只要model有东西,就不会出现问题;

     

    解决方法1:model标记:

         (1) 从复用队列取出cell:

           InvesmentRedCell *cell = [tableView cellForRowAtIndexPath:indexPath];

         (2) 对当前显示的cell做一些处理,比如在点击的时候让其他cell中控件颜色变化等等:
            for (InvesmentRedCell *tempCell in _tableviewInvestmentRed.visibleCells)
            {
                tempCell.imgUse.highlighted = NO;
                
                tempCell.lblAmount.textColor = [UIColor colorWithRed:0.53 green:0.53 blue:0.53 alpha:1];
                
                tempCell.lblExpiredDate.textColor = [UIColor colorWithRed:0.53 green:0.53 blue:0.53 alpha:1];
            }

          (3)遍历数组,把model的选择状态改变
            for (MyWelfareModel *tempModel in _arrInvesmentRed)
            {
                tempModel.isSelect = NO;
            }

          (4) 正常处理点击后的状态:
            tempDataModel.isSelect = YES;
            cell.imgUse.highlighted = YES;
            cell.lblAmount.textColor = _selectedColor;
            cell.lblExpiredDate.textColor = _selectedColor;
            _couponId = tempDataModel.couponid;
            _selectedModel = tempDataModel;
            NSInteger amount = [_amountCell.valueTextFied.text integerValue] - _selectedModel.amount.integerValue;
            if (amount >= 0) {
                _strInputAmout = [NSString stringWithFormat:@"%ld",(long)amount];
            } else {
                _strInputAmout = @"0";
            }
    解决办法2:移除点击事件重新添加
        (1)取出复用队列中的cell

        VideoDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer forIndexPath:indexPath];
        
        VideoDetailModel *model = self.modelArray[indexPath.row];
        cell.model = model;

        (2)移除取出cell上的button点击事件
        [cell.like removeTarget:self action:@selector(likeButton:) forControlEvents:UIControlEventTouchUpInside];

        (3)重新添加事件
        [cell.like addTarget:self action:@selector(likeButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.like.tag = 2000+indexPath.row;
        
        return cell;

     

    解决办法3:使用block解决:

    (1)cell中声明一个block属性

      @property (nonatomic, copy) void (^moreButtonClickedBlock)(NSIndexPath *indexPath);

    (2)cell中添加button事件

        _moreButton = [UIButton new];
        [_moreButton setTitle:@"显示全部" forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:14];

    (3)button点击事件

    - (void)moreButtonClicked
    {
        if (self.moreButtonClickedBlock) {
            self.moreButtonClickedBlock(self.indexPath);
        }
    }

    (4)布局cell时

            DemoVC9Cell *cell = [tableView dequeueReusableCellWithIdentifier:kDemoVC9CellId];

       ||记得传入点击位置,在第三步block回传
        cell.indexPath = indexPath;
        __weak typeof(self) weakSelf = self;
        if (!cell.moreButtonClickedBlock) {
            [cell setMoreButtonClickedBlock:^(NSIndexPath *indexPath) {
                Demo9Model *model = weakSelf.modelsArray[indexPath.row];
                model.isOpening = !model.isOpening;
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }];
        }
        cell.model = self.modelsArray[indexPath.row];
        return cell;

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-8-8 00:07 , Processed in 0.058150 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表