zoukankan      html  css  js  c++  java
  • iOS TableView实现单选checkmark功能

    相信大家在做项目时有遇到需要实现这种功能---实现单选某一个cell表示选中

    这个功能的实现只需要在两个方法中code即可

    首选我们公开一个属性 

    @property(nonatomic,strong)NSIndexPath *lastPath;并且对其synthesize

    主要是用来接收用户上一次所选的cell的indexpath

    第一步:在cellForRowAtIndexPath:方法中实现如下代码

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

          NSInteger row = [indexPath row];

          NSInteger oldRow = [lastPath row];

           if (row == oldRow && lastPath!=nil) {

            cell.accessoryType = UITableViewCellAccessoryCheckmark;

           }else{

             cell.accessoryType = UITableViewCellAccessoryNone;

        }

    }

     

    第二步:在didSelectRowAtIndexPath:中实现如下代码

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

        int newRow = [indexPath row];

        int oldRow = (lastPath !=nil)?[lastPath row]:-1;

        if (newRow != oldRow) {

            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

            newCell.accessoryType = UITableViewCellAccessoryCheckmark;

            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastPath];

            oldCell.accessoryType = UITableViewCellAccessoryNone;

            lastPath = indexPath;

         }

        [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }

     

    Ok,可以收工了,这样实现之后的效果是每次单击一个cell会做一个选中的标志并且托动表视图时也不会出现checkmark的复用

    希望对初学者有帮助到!

  • 相关阅读:
    西交应用统计学(四)
    SPSS非参数检验
    并查集实现
    二叉树遍历非递归算法
    算法导论——渐近符号、递归及解法
    SPSS均值过程和T检验
    二维数组的查找及向函数传递二维数组问题
    printf()的格式
    C++ string类型的读写
    替换字符串中的空格
  • 原文地址:https://www.cnblogs.com/billy-chou/p/3941746.html
Copyright © 2011-2022 走看看