删除所有的cell:
//找到所有的indexPath
NSArray *arr = [self.tableView indexPathsForRowsInRect:CGRectMake(0, 0, self.view.frame.size.width, self.tableView.contentSize.height)];[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.tableView selectRowAtIndexPath:obj animated:YES scrollPosition:UITableViewScrollPositionNone];
}];
//拿到现在是选择状态的indexP数组。。
/*
**这个属性方便了太多!
*/
NSArray *arr = self.tableView.indexPathsForSelectedRows;
//此处从数组删除注意:按照arr 顺序删除会造成越界崩溃、、
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
for (NSIndexPath *indexP in arr) {
[set addIndex:indexP.row];
}
[self.dataArr removeObjectsAtIndexes:set];
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
二.在cell处于非编辑状态的时候 滑动删除:
1,首先允许用户编辑:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
2, 滑动点击删除按钮后执行这个方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.dataArr removeObjectAtIndex:indexPath.row];
[self.tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
三.在多选状态下点击选择的cell之后 点击确定按钮之后执行:
-(IBAction)shureAction:(id)sender{
//拿到现在是选择状态的indexP数组。。
/*
**这个属性方便了太多!
*/
NSArray *arr = self.tableView.indexPathsForSelectedRows;
//此处从数组删除注意:按照arr 顺序删除会造成越界崩溃、、
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
for (NSIndexPath *indexP in arr) {
[set addIndex:indexP.row];
}
[self.dataArr removeObjectsAtIndexes:set];
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
// [self.tableView reloadData];
NSLog(@"seleteArr = %@",arr);
}