zoukankan      html  css  js  c++  java
  • 解决cell循环利用造成的重复勾选

    @interface ProfessionViewController (){
        NSMutableArray  *_professionArray;//cell模型数组
        NSMutableArray  *_selectArray;    //已选中的cell数组
    }
    

    - (void)viewDidLoad{

      //初始化已选中的cell数组
        _selectArray = [NSMutableArray array];
    }
    #pragma mark 当cell被选中时调用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //获取当前选中的cell模型 Profession *profession = _professionArray[indexPath.section]; //获取当前选中的cell模型中的title NSString *title = profession.trade[indexPath.row];
      if ([_selectArray containsObject:title]) {
            //之前是选中,现在取消选中
            [_selectArray removeObject:title];
        }else{
            //之前未选中,现在选中
            [_selectArray addObject:title];
        }
      //刷新被选中的cell  
      [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }

    #pragma mark  设置每一行cell显示的内容

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        //定义循环利用标识

        static NSString *identifier = @"profession";

        _cell = [tableView dequeueReusableCellWithIdentifier:identifier];

        //如果cell为空

        if (!_cell) {

            _cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

        }

        //取得cell模型数组         

        Profession *profession = _professionArray[indexPath.section];

        //取得cell模型数组中的title

        NSString *title = profession.trade[indexPath.row];

        //取得行业并设置给cell

      _cell.textLabel.text = title;

        //判断当前title是否在已选中的数组中

        if ([_selectArray containsObject:title]) {

            //打钩

            _cell.accessoryType = UITableViewCellAccessoryCheckmark;

        }else{

            //不打钩

            _cell.accessoryType = UITableViewCellAccessoryNone;

        }

        return _cell;

    }



  • 相关阅读:
    数据结构-栈
    virtualenvwrapper 的安装和使用
    MySQL命令行本地登陆,远程登陆MySQL 的快捷键
    关于mysql8授权的问题,mysql萌新小白采坑记录
    史上最直接小白式的Sourcetree的分支创建与合并
    验证两个集合是否相等的方法
    VS开发工具的常用插件
    C# 数据库并发的解决方案(通用版、EF版)
    Jquery实现div左右重复来回走动
    描述符的应用
  • 原文地址:https://www.cnblogs.com/hw140430/p/3749039.html
Copyright © 2011-2022 走看看