iOS开发:UITableView编辑——cell的删除和移动
2015-09-06 13:19
459 查看
iOS开发:UITableView编辑——cell的删除和移动
首先在昨天的基础上添加一个sectionArray数组(详见《UI第九天:UITableView简单介绍》)
self.sectionArray=
[NSMutableArrayarrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",nil];
1、section的常用方法:(效果见下图)
#pragma mark section的个数(分区个数)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return_sectionArray.count;
}
#pragma mark 显示section分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_sectionArrayobjectAtIndex:section];
}
#pragma mark 分区标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
2、添加右侧索引栏:(效果见下图)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _sectionArray;
}
//当然也可以通过设置相关的属性来使索引栏与众不同
//修改索引栏的背景颜色和显示颜色:
_tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0.38
green:0.78 blue:0.95 alpha:1];
_tableView.sectionIndexColor = [UIColor blueColor];
3、尾视图(效果见下图)
#pragma mark 尾视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
#pragma mark 尾视图标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return@"Hello Girl";
}
4.自定义Header视图(效果见下图)
#pragma mark 自定义Header视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//找到当前section的标题
NSString *sectionString = [self tableView:tableView titleForHeaderInSection:section];
//自定义label(对label可以编辑自己喜欢的样式)
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
label.textAlignment = 1;
label.font = [UIFont fontWithName:@"Zapfino" size:20];
label.text = sectionString;
//创建view作为label的父视图
UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[backView addSubview:label];
[label release];
return backView;
}
*以上各方法效果如下图:
5、cell的删除
#pragma mark 编辑触发事件(将这个方法给UINavigationController右上角的"编辑",让它触发这个事件)
- (void)beginEditing{
if (!_flag) {
[_tableView setEditing:YES animated:YES];
_flag = 1;
}else{
[_tableView setEditing:NO animated:YES];
_flag = 0;
}
}
//删除四步走:
#pragma mark 开启编辑动画
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[_tableView setEditing:!_tableView.editing animated:animated];
}
#pragma mark 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 选择编辑样式 (删除和插入,删除使用的较多)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 开始编辑(删除)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//判断编辑样式(删除还是插入)
if (editingStyle == UITableViewCellEditingStyleDelete) {
//从对应的cell移除数据
[_dataSource removeObjectAtIndex:indexPath.row];
//刷新TableView
[_tableView reloadData];
}
}
6、cell的移动
//移动三步走(事件还是有上面beginEditing方法触发(点击编辑))
#pragma mark 开启编辑动画
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[_tableView setEditing:!_tableView.editing animated:animated];
}
#pragma mark
允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark
开始移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//获取起始cell的下标
NSInteger fromIndex = sourceIndexPath.row;
//获取目的cell的下标
NSInteger toIndex = destinationIndexPath.row;
//提取出起始cell的数据
//获取元素
id oldData = [_dataSource objectAtIndex:fromIndex];
//删除就得数据
[_dataSource removeObject:oldData];
//添加到新的位置
[_dataSource insertObject:oldData atIndex:toIndex];
}
*删除与移动效果图
首先在昨天的基础上添加一个sectionArray数组(详见《UI第九天:UITableView简单介绍》)
self.sectionArray=
[NSMutableArrayarrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",nil];
1、section的常用方法:(效果见下图)
#pragma mark section的个数(分区个数)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return_sectionArray.count;
}
#pragma mark 显示section分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_sectionArrayobjectAtIndex:section];
}
#pragma mark 分区标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
2、添加右侧索引栏:(效果见下图)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _sectionArray;
}
//当然也可以通过设置相关的属性来使索引栏与众不同
//修改索引栏的背景颜色和显示颜色:
_tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0.38
green:0.78 blue:0.95 alpha:1];
_tableView.sectionIndexColor = [UIColor blueColor];
3、尾视图(效果见下图)
#pragma mark 尾视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
#pragma mark 尾视图标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return@"Hello Girl";
}
4.自定义Header视图(效果见下图)
#pragma mark 自定义Header视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//找到当前section的标题
NSString *sectionString = [self tableView:tableView titleForHeaderInSection:section];
//自定义label(对label可以编辑自己喜欢的样式)
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
label.textAlignment = 1;
label.font = [UIFont fontWithName:@"Zapfino" size:20];
label.text = sectionString;
//创建view作为label的父视图
UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[backView addSubview:label];
[label release];
return backView;
}
*以上各方法效果如下图:
5、cell的删除
#pragma mark 编辑触发事件(将这个方法给UINavigationController右上角的"编辑",让它触发这个事件)
- (void)beginEditing{
if (!_flag) {
[_tableView setEditing:YES animated:YES];
_flag = 1;
}else{
[_tableView setEditing:NO animated:YES];
_flag = 0;
}
}
//删除四步走:
#pragma mark 开启编辑动画
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[_tableView setEditing:!_tableView.editing animated:animated];
}
#pragma mark 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 选择编辑样式 (删除和插入,删除使用的较多)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 开始编辑(删除)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//判断编辑样式(删除还是插入)
if (editingStyle == UITableViewCellEditingStyleDelete) {
//从对应的cell移除数据
[_dataSource removeObjectAtIndex:indexPath.row];
//刷新TableView
[_tableView reloadData];
}
}
6、cell的移动
//移动三步走(事件还是有上面beginEditing方法触发(点击编辑))
#pragma mark 开启编辑动画
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[_tableView setEditing:!_tableView.editing animated:animated];
}
#pragma mark
允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark
开始移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//获取起始cell的下标
NSInteger fromIndex = sourceIndexPath.row;
//获取目的cell的下标
NSInteger toIndex = destinationIndexPath.row;
//提取出起始cell的数据
//获取元素
id oldData = [_dataSource objectAtIndex:fromIndex];
//删除就得数据
[_dataSource removeObject:oldData];
//添加到新的位置
[_dataSource insertObject:oldData atIndex:toIndex];
}
*删除与移动效果图
相关文章推荐
- 聊聊并发——ConcurrentLinkedQueue的实现原理分析
- ui概述
- iOS-UILabel 自动换行
- Parameter 'uidList' not found. Available parameters are [list]
- Arduino DS18B20温度检测
- ecshop编辑器fckeditor换百度ueditor编辑器教程
- poj 2778 DNA Sequence (ac自动机+矩阵快速幂优化dp)
- String、StringBuilder
- 1099. Build A Binary Search Tree (30)
- this class is not key value coding-compliant for the key imageView的崩溃问题
- jquey.fullpage左右无缝轮播
- VBA 使用QueryTables 中文乱码的处理
- std::deque
- [性能]StringBuilder与StringBuffer的区别(转)
- UIKit;多任务
- Implement Stack using Queues 两个队列实现一个栈
- 实现当tableview当向上滚动时,头部添加的view看上去不随向上滚动的效果
- [LintCode]Segment Tree Build 构造线段树
- UITextView的动态适应高度处理
- SOAPUI系列14- SOAPUI 数据生成器组件使用方法