1.单元的删除和添加执行的步骤(以下方法都是tableView的数据源和代理方法)
1.将表格变成可编辑状态
self.tableView.editing = YES;
2.设置指定单元能否被编辑,这一步不设置默认都能被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return YES;
}
return YES;
}
3.设置单元格编辑时的样式 UITableViewCellEditingStyleInsert插入
UITableViewCellEditingStyleDelete删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < 3) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}
4.执行删除操作 特别提醒 :在进行删除和添加操作时,第一步首先将装载tableView的数据的数组进行添加或者删除,第二步删除,添加表格,或者刷新tableView。因为在执行删除、添加或者刷新表格会重新调用tableView的数据源方法来设置表格中的组数,行数等数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
//1.从数组中移除数据
[self.dataSource[indexPath.section] removeObjectAtIndex:indexPath.row];
//2.删除表格单元
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
//还有一种是刷新tableView
// [self.tableView reloadData];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
//1.将新的元素添加到数据源数组中
[self.dataSource[indexPath.section] insertObject:@"新的元素" atIndex:indexPath.row];
//2.在表格追加单元 或者刷新表格
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft] ;
// [tableView reloadData];
}
}
2.单元删除/追加时的动画
UITableViewRowAnimationFade,以淡出动画形式删除单元格 以淡入动画形式追加单元格
UITableViewRowAnimationRight,被删除单元向右侧移动,被追加单元从右侧出现
//以下类推
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
3.横向滑动进行单元删除
1.单元格的编辑状态都是UITableViewCellEditingStyleDelete(这也是默认状态)
2.单元格是不可编辑状态
4.删除按钮名称的变更
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"讨厌,不要删除人家。。。";
}
5.单元的移动
1.单元格移动调用的方法,这个方法为空也行,一般在改方法内部实现数据资源的顺序变更
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
}
2.设置单元格的能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//显示每组最后一行不能移动
if (indexPath.row == [self.dataSource[indexPath.section] count] - 1) {
return NO;
}
return YES;
}
3.限制单元格移动的范围
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//限制不能向每组的最后一行后面移动
if ([self.dataSource[proposedDestinationIndexPath.section] count] > proposedDestinationIndexPath.row + 1) {
return proposedDestinationIndexPath;
}else{
return sourceIndexPath;
}
}
6.利用编辑按钮的属性,对单元格进行编辑
1.向导航栏添加编辑按钮
//向导航栏添加编辑按钮
self.navigationItem.leftBarButtonItem = [self editButtonItem];
2.点击导航栏的编辑按钮时会调用setEdit事件,重写SetEdit事件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
self.tableView.editing = editing;
}
7.多个单元同时编辑
需要注意的是这个操作必须放在 [self.tableView beginUpdates];和 [self.tableView endUpdates];之间执行
//删除的单元格的数组
NSArray *forDeleting = @[[NSIndexPath indexPathForRow:0 inSection:0],[NSIndexPath indexPathForRow:0 inSection:1]];
[self.tableView beginUpdates];
//1.检测要删除的数据是否符合要求
//2.移除数据源要删除的元素
for (NSIndexPath *indexPath in forDeleting) {
[self.dataSource[indexPath.section] removeObjectAtIndex:indexPath.row];
}
//3.进行删除
[self.tableView deleteRowsAtIndexPaths:forDeleting withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];