您的位置:首页 > 产品设计 > UI/UE

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];
}
*删除与移动效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: