首先說一下編輯的代理方法:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
實現(xiàn)以上代理方法就可以實現(xiàn)刪除;
如果沒有分組的話
// 刪除數(shù)據(jù)源
[self.addressArr removeObjectAtIndex:indexPath.section];
// 刪除cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
可是如果你分組的話,這樣實現(xiàn)刪除就要有bug啦!
所以需要添加一下代碼,實現(xiàn)分組的刪除:
// 更新
[tableView beginUpdates];
[self.addressArr removeObjectAtIndex:indexPath.section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// 結(jié)束更新
[tableView endUpdates];
deleteSections:indexSetWithIndex:是用來刪除沒有cell的分組;本人的需求是一個分組只有一個數(shù)據(jù)所以不需要判斷;如果你是多個數(shù)據(jù)就要判斷一下是不是最后一個cell,如果只有一個cell就要執(zhí)行這個方法啦;