zoukankan      html  css  js  c++  java
  • 表视图单选和多选的实现

    1.实现思路:

    我的直接思路是修改某一个Cell的样式即可,

    那么修改样式需要通过修改对应的数据,

    从这里可以推断我们需要给Cell对应的数据设置一个标志位,

    当选中的时候来修改该标志位刷新那一行即可

    如果是单选实现稍微复杂一些:

    单选需要设置一个属性来保存上一次选中的行,

    待选中新的行之后需要修改该行,不断维护

    2.单选实现的简单代码如下:

     /*实现单选
        NSIndexPath *temp = self.lastSelected;//暂存上一次选中的行
        if(temp && temp!=indexPath)//如果上一次的选中的行存在,并且不是当前选中的这一样,则让上一行不选中
        {
            SelectAssertModel *model = self.selectArray[temp.row];
            
            model.ifSelected = NO;//修改之前选中的cell的数据为不选中
            [tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
        }
        self.lastSelected = indexPath;//选中的修改为当前行
        SelectAssertModel *model = self.selectArray[temp.row];
        model.ifSelected = YES;//修改这个被选中的一行choon
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//重新刷新这一行
        
         */

    3.多选实现的简单代码如下:

     SelectAssertModel *model = self.selectArray[indexPath.row];
        
        if (!model.ifSelected) {
            model.ifSelected = YES;//修改这个被选中的一行
            
        }
        else{
            model.ifSelected = NO;//修改这个被选中的一行
    
        }
         [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
    }

    4.统计选择的数据:

    //"确定"按钮被点击
    - (void)rightItemDidClicked:(UIBarButtonItem *)item {
    
        //每次进来记得清除数组
        [self.selectedArray removeAllObjects];
        for (SelectAssertModel *model in self.selectArray) {
            if (model.ifSelected) {
                
                [self.selectedArray addObject:model];
            }
        }
        NSLog(@"count = %ld",self.selectedArray.count);
        NSLog(@"count = %@",self.selectedArray);
    
    }
  • 相关阅读:
    机器学习中的距离度量
    ubuntu 安装JDK
    pandas 代码
    pandas 常用统计方法
    python内置函数map/reduce/filter
    详解SQL Server连接(内连接、外连接、交叉连接)
    什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?
    数据挖掘算法源代码:很好的参考资料
    python linecache模块读取文件用法详解
    python读取文件指定行
  • 原文地址:https://www.cnblogs.com/pengsi/p/5552454.html
Copyright © 2011-2022 走看看