zoukankan      html  css  js  c++  java
  • 如何对tableview进行自定义多选

    前言:

      很多入门不久的程序员或许都会遇到系统自带的tableview多选时cell选择时不能选择自己想要的点击时的颜色或者图片,这让初级开发者们很烦恼。今天,我试着花了几个小时的时间用了自己的想法,去做了自定义的tableview的多选,仅供参考使用,如有觉得写的不好的,可以随时交流,谢谢。


    1.自定义cell,假设cell总共有三个控件;

      (1)_selecedImgIcon则为设置多选时才出现的我们所想自定义的selected控件,设置这个控件时需要把它设置在视图左边,点击多选时向右推才出现

      _selecedImgIcon.frame   = CGRectMake(-48 * Scale_width, 56 * Scale_heigh, 46 * Scale_heigh, 46 * Scale_heigh);

      

        _noteImageV.frame       = CGRectMake(22 * Scale_width, 38 * Scale_heigh, 82 * Scale_heigh, 82 * Scale_heigh);

         _noteTittle.frame       = CGRectMake(124 * Scale_width, 42 * Scale_heigh, 200 * SCALEX, 30 * Scale_heigh);

         _noteTime.frame         = CGRectMake(124 * Scale_width, 96 * Scale_heigh, 200 * SCALEX, 20 * Scale_heigh);

      由于cell的contentView是只读的,不可以改变其frame,因此,需要在contentView上加一个contentV,把所有需要用的控件都放在上面,需要多选时再将contentV向左推,将多选时的控件显示出来。所以记得selected控件的x一定要置于左端,即x坐标要是负的

    2.声明一个bool属性的editing,初始时为NO;

      BOOL _editing;

      _editing = NO;

    3.添加一个多选的button,为button添加一个target;

      @selector(onMultipleChoice)

    4.实现onMultipleChoice这个方法;(记得先设置好未选择时的图片) 

      // 设置多选时contentV的x需要往右移多少才能将cell推出来(自己算)

      float contentX = editing ? 70 * Scale_width : 0;

      // 获取所有的cell,并设置动画效果将cell推出

      for (int i = 0; i < _array.count; i ++)

       {  // 这里假设只有一个section

            NoteTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

                cell.contentV.frame  = CGRectMake(contentX, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);

                NSLog(@"%f",cell.contentView.center.x);

                [cell layoutIfNeeded];

            } completion:^(BOOL finished) {

            }];

        }

    5.实现两个tableview的代理方法:分别为选中时和未选中时;

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

     

    {

      /**********

      此处写选中时的数据操作

      ***********/

     

        if (_editing)

     

        {

     

            _cell = [tableView cellForRowAtIndexPath:indexPath];

     

            _cell.selecedImgIcon.image  = [UIImage imageNamed:@"privacy_selected02"];

     

            return;

     

        }

     

    }

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)

    {

      /**********

      此处写未选中时的数据操作

      ***********/

        _cell = [tableView cellForRowAtIndexPath:indexPath];

        _cell.selecedImgIcon.image  = [UIImage imageNamed:@"privacy_selected01"];

    }

    6.实现后如图所示:

     

    7.结语:

      希望各位大神们,看了有什么想法或有什么建议的可以跟我聊聊,我相信交流永远是成长最快的。

     

     

  • 相关阅读:
    jenkins集群(三) -- master和slave配置git
    jenkins集群(二)(master --> slave) -- allure自动化测试报告部署
    jenkins集群(一) -- 在Linux上部署
    APP自动化 -- contexts(上下文切换)
    APP自动化 -- 滑动解锁、滑动验证
    APP自动化 -- 坐标获取和点击
    APP自动化 -- 获取toast元素的文本内容
    APP自动化 -- MobileBy
    Python学习-基础数据类型补充操作、bytes类型
    Kafka-生产者、消费者、自定义分区器
  • 原文地址:https://www.cnblogs.com/destiLaugh/p/5846016.html
Copyright © 2011-2022 走看看