在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作

所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便

当UITableView进入编辑模式时,默认会将所有的单元行向右缩进。不过缩进后多出的空间是否显示系统图标以及显示哪种图标却是由开发者通过代理回调函数自己来定义。这样的话,如果我们不让它显示任何东西,而是用作显示多选中的一个选中标示的图标,多选单元行的重要思路就搞定了。

(1)新建一个继承自UITableViewCell的子类取名为MultiSelectionCell,这个类有两个成员变量:用于标记是否选中状态的图标和布尔值,并且有必要提供一个通过外部来选择或者反选当前单元行的接口。。头文件声明如下:

 @interface HBMultiSelectionCell : UITableViewCell
{
@private
//是否选中状态的图标
UIImageView *_imgSelectionMark;
//是否选中状态的变量
BOOL _isSelected;
} @property (nonatomic,assign) BOOL checked; -(void)setChecked:(BOOL)checked;

(2)完成MultiSelectionCell的剩余代码,当被调用setEditing时,需要将一个是否选中的图标以和单元行右缩进相似的动画显示出来。反则也需要以相似的动画隐藏起来。在进入编辑状态时,单元行如果已经被选中,不仅需要在左侧显示响应的选中图标,也需要为单元行设置一个背景色来和未选中的单元行加以区分。

实现文件的代码如下:

 @implementation HBMultiSelectionCell
@synthesize checked = _isSelected; //两种情况会调用
//(1)Cell首次显示
//(2)tableview执行了setEditing:animated -(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//和当前状态相同
if(self.editing == editing)
{
return;
} //不要破坏原本的系统动作
[super setEditing:editing animated:animated]; //进入编辑状态
if(editing)
{
//背景视图生成,以准备设置选中和未选中的不同背景色
self.backgroundView=[[UIView alloc]init];
self.backgroundView.backgroundColor=[UIColor whiteColor]; //表示选中与否的图片,位置和编辑状态的控件相同,同样放在最左边
//不过考虑到进入编辑状态,cel是一个从左向右的动画移动,所以初始化这个图片也放在负X位置,准备从左向右做一个动画来显示
if(!_imgSelectionMark)
{
_imgSelectionMark = [[UIImageView alloc]initWithFrame:CGRectMake(-14.5f, CGRectGetHeight(self.bounds)/-14.5f, 29.0f, 29.0f)]; _imgSelectionMark.alpha=0.0f;
[self addSubview:_imgSelectionMark];
} //更新选中与否的界面显示
[self setChecked:_isSelected]; //从左向右的移动且显示动画
[UIView animateWithDuration:0.3f animations:^{
_imgSelectionMark.frame=CGRectMake(6.0f, CGRectGetMinY(_imgSelectionMark.frame), CGRectGetWidth(_imgSelectionMark.frame), CGRectGetHeight(_imgSelectionMark.frame));
_imgSelectionMark.alpha=1.0f;
}];
}
else
{
//背景视图销毁,大家都变成普通的颜色,即默认白色
self.backgroundView=nil; //文字颜色变回来
self.textLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textColor = [UIColor grayColor]; //从右向左的移动且隐藏动画
[UIView animateWithDuration:0.3 animations:^{
_imgSelectionMark.frame=CGRectMake(-14.5f, CGRectGetMinY(_imgSelectionMark.frame), CGRectGetWidth(_imgSelectionMark.frame), CGRectGetHeight(_imgSelectionMark.frame));
_imgSelectionMark.alpha=0.0f;
}];
}
} -(void)setChecked:(BOOL)checked
{
//选中
if(checked)
{
//勾选的图标
_imgSelectionMark.image = [UIImage imageNamed:@"check.png"];
//勾选状态的背景颜色
self.backgroundView.backgroundColor = [UIColor colorWithRed:38.0/255.0f green:96.0f/255.0f blue:211.0f/255.0f alpha:1.0];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
}
//反选
else
{
//反选的图标
_imgSelectionMark.image = [UIImage imageNamed:@"uncheck.png"];
self.backgroundView.backgroundColor = [UIColor whiteColor]; self.textLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
//需要记录到成员量中
_isSelected=checked;
}

(3)将MutiSelectionCell加载到表视图中

由于大部分的显示效果都和删除功能相似,所以可以新建一个继承自HBDeleteViewControll的表视图控制器取名为HBMutiSelectionViewController。在实现文件中,需要注意将删除功能规避,并且需要开启“allowsSelectionDuringEditing属性”以允许在编辑状态进行单元行选择,这样就可以在单元行选择的代理回调函数中,调用响应的MutiSelectionCell对象进行选中与否的更新和设置,代码如下:

 -(void)initUI
{
[super initUI]; //为了多选
self.tableView.allowsSelection = YES;
self.tableView.allowsSelectionDuringEditing = YES;
} #pragma mark-
#pragma mark TableView data Source
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"MultiTableViewCellId";
HBMultiSelectionCell *cell = (HBMultiSelectionCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[HBMultiSelectionCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
} HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row];
if(onePlayer)
{
cell.textLabel.text = onePlayer.name;
cell.detailTextLabel.text = onePlayer.role;
cell.imageView.image=[UIImage imageNamed:@"gaolin.jpeg"]; //进入编辑状态,由于要多选,所以选择好的cell的背景颜色需要和为选择的cell的背景颜色有区别
//所以这里cell上的元素需要背景设置成透明(默认白色),以不影响cell选中状态时背景颜色的显示效果
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
return cell;
} #pragma mark-
#pragma mark Table View delegate
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//不要显示任何编辑图标
return UITableViewCellEditingStyleNone;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //编辑中状态的cell选择
if(self.tableView.editing)
{
if(!self.datasource || indexPath.section >= self.datasource.count)
{
return;
} //更新cell的选择状态
HBMultiSelectionCell *cell = (HBMultiSelectionCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.checked= !cell.checked;
[cell setChecked:cell.checked];
}
}

运行效果如图:

IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)的更多相关文章

  1. IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)

    表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...

  2. iOS 表视图(UITableVIew)的使用方法(1)表视图的示例

    表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等.配合UINavigationController的导 ...

  3. IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)

    默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...

  4. IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)

    除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...

  5. IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示

    当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...

  6. IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示

    我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...

  7. IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元

    UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...

  8. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  9. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

随机推荐

  1. poj 1469(二分图 最大匹配)

    这道题让我认识到了c++cin,cout确实会使其超时,还是我用的c printf吧 #include<cstdio> #include<iostream> #include& ...

  2. java 乱码详解_jsp中pageEncoding、charset=UTF -8"、request.setCharacterEncoding("UTF-8")

    http://blog.csdn.net/qinysong/article/details/1179480 java 乱码详解__jsp中pageEncoding.charset=UTF -8&quo ...

  3. hadoop笔记之hdfs

    1.HDFS设计基础与目标 1.HDFS设计基础与目标 (1)硬件错误是常态,因此需要冗余. (2)流式数据访问.即数据批量读取而非随机读写,Hadoop擅长做的是数据分析而不是事务处理. (3)大规 ...

  4. linux杂记(五)正确关机方法(shutdown,reboot,init,halt)

    前言:由于在linux底下,每个程序(或者说是服务)都是在背景下运行的,因此,在你看不到的屏幕背后其实可能有相当多人同时在你的主机上面工作,如果 你直接按下电源开关来关机,则可能导致其他人的数据就此中 ...

  5. Hadoop2.0安装

    http://blog.csdn.net/samhacker/article/details/18802223 http://blog.csdn.net/crazyhacking/article/de ...

  6. c# winform读取xml创建菜单

    动态创建菜单使得程序灵活性大大增加,本文根据读取xml文件中的配置菜单项来动态创建菜单,代码如下: using System; using System.Collections.Generic; us ...

  7. 基于xml文件实现系统属性配置管理

    文章标题:基于xml文件实现系统属性配置管理 . 文章地址: http://blog.csdn.net/5iasp/article/details/11774501 作者: javaboy2012 E ...

  8. 刷新指定行或区 cell

    //一个section刷新 NSIndexSet *indexSetA = [[NSIndexSet alloc]initWithIndex:3];    //刷新第3段 [tableview rel ...

  9. SGU 200 Cracking RSA (高斯消元)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出m个整理,因子全部为前t个素数.问有多少 ...

  10. SSD磁盘,CPU居高不下,高并发的情况下,是不是mysql解析器耗费的cpu资源高?

    你看看我做的实验,这个user表是300多W纪录,普通磁盘下,消耗时间最多的是Copy to tmp table 0.81秒,当然在ssd下,这个可以减少很多很多的,第二高就是sending data ...