• 解决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;

    }



  • 相关阅读:
    char、varchar、nchar、nvarchar的区别
    linux和windows下分别如何查看电脑是32位的还是64位?
    HP-Unix安装Memcache问题
    安装GCC-4.6.1详细教程
    JSTL 核心标签库 使用
    JSP && EL表达式
    UNIX环境高级编程——标准IO-实现查看所有用户
    UNIX环境高级编程——环境变量表读取/添加/修改/删除
    UNIX网络编程——进程间通信概述
    UNIX网络编程——通过UNIX域套接字传递描述符和 sendmsg/recvmsg 函数
  • 原文地址:https://www.cnblogs.com/hw140430/p/3749039.html
Copyright © 2020-2023  润新知