首页 技术 正文
技术 2022年11月18日
0 收藏 356 点赞 2,929 浏览 2123 个字

前言:

  很多入门不久的程序员或许都会遇到系统自带的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.实现后如图所示:

如何对tableview进行自定义多选

如何对tableview进行自定义多选

如何对tableview进行自定义多选

7.结语:

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898