IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作
所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便
当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)表视图的编辑功能(多选)的更多相关文章
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等.配合UINavigationController的导 ...
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
默认的,如果表视图支持编辑,那用户可以通过两种方式来删除某些行,其一为单击左侧的红色按钮后行右侧显示“Delete”按钮,其二为在单元行上的手指向左滑动,“Delete”按钮也会出现供用户单击.无论哪 ...
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
当数据量特别大时,简单地以role进行分段,对实际查找的效率提升并不大.就像上一节开头所说,开发者可以根据球员名字的首字母进行分段,且分成26段.由于段数较多,可以使用UITableView的索引机制 ...
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
我们可以采用名字分段法,名字分段会在之后的小节中显示,这是转而使用球员的角色分段发,以最直接的入手点讲解好UITableView的分段使用方法.本节示例时基于上节的SimpleTableViewCon ...
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
UITableViewCell的自定义往往需要自建一个UITableViewCell的子类后进行作业.开发者可以选择通过xib或者直接在UITableViewCell的布局中进行UITableView ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
随机推荐
- 一年三篇IF大于7的牛人告诉你怎么写SCI
一年三篇IF大于7的牛人告诉你怎么写SCI 1 研究生必备四本 俗话说好记性不如烂笔头,所以一定要首先养成做笔记的好习惯!作为研究生下面这几个本子是必不可少的: 1.实验记录本(包括试验准备本),这当 ...
- 解决Metadata file does not match checksum错误
1.清空缓存执行: # yum clean all 先把就的缓存数据都去掉. 2.下载metadata和校验数据先进入yum对应的目录,再下载: # cd /var/cache/yum/rpmforg ...
- Winform中上传、下载文件选择打开文件的位置
打开将要上传的文件 var fileName="";OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Mul ...
- class A<T> where T:class 这个泛型类中的Where T:class什么意思
这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct T必须是一个结构类型 where T : cla ...
- mina 实例(转载)
mina:http://mina.apache.org/ 原文:http://maosheng.iteye.com/blog/1891665 大并发量socket 通信框架MINA介绍 博客分类: J ...
- virtualBox文件共享
具体过程,可以参考: http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html 共享命令:sudo mount -t vboxsf ...
- BOM 浏览器对象模型学习
window对象属性 innerWidth/innerHeight 浏览器窗口的内部宽度与高度 outerWidth/outerHeight 浏览器的外部宽度与高度 length window.fra ...
- MySQL 创建用户与修改密码
创建用户的 3 方法: 1 .create user userName identifed by 'yourPassword'; 2. grant select on *.* to userName@ ...
- Bluetooth 2.1+EDR是什么
目前应用最为广泛的是 Bluetooth 2.0+EDR标准,该标准在2004年已经推出,支持Bluetooth 2.0+EDR标准的产品也于2006年大量出现.虽然Bluetooth 2.0+EDR ...
- Jsoup代码解读之四-parser
Jsoup代码解读之四-parser 作为Java世界最好的HTML 解析库,Jsoup的parser实现非常具有代表性.这部分也是Jsoup最复杂的部分,需要一些数据结构.状态机乃至编译器的知识.好 ...