批量删除
- (void)viewDidLoad {
[super viewDidLoad];
// self.tableView.allowsMultipleSelection = YES;
// 告诉tableView在编辑模式下可以多选
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.deletedButton.hidden = YES;
}
#pragma mark - 按钮的点击
- (IBAction)MultipleRemove {
// 进入编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
self.deletedButton.hidden = !self.tableView.isEditing;
}
- (IBAction)remove {
// 千万不要一边遍历一边删除,因为每删除一个元素,其他元素的索引可能会发生变化
NSMutableArray *deletedWine = [NSMutableArray array];
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
[deletedWine addObject:self.wineArray[indexPath.row]];
}
// 修改模型
[self.wineArray removeObjectsInArray:deletedWine];
// 刷新表格
// [self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
---------------------------
// 获取要删除的酒模型
NSMutableArray *deletedWine = [NSMutableArray array];
for (NSIndexPath *indexPath in self.seletedIndexPath) {
[deletedWine addObject:self.wineArray[indexPath.row]];
}
// 删除模型
[self.wineArray removeObjectsInArray:deletedWine];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:self.seletedIndexPath withRowAnimation:UITableViewRowAnimationAutomatic];
// 清空数组
[self.seletedIndexPath removeAllObjects];
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 修改模型
LZJWine *wine = self.wineArray[indexPath.row];
if (wine.isCheched) { // 之前是打钩的,取消打钩
wine.checked = NO;
[self.seletedIndexPath removeObject:indexPath];
} else { // 之前不是打钩的,现在打钩
wine.checked = YES;
[self.seletedIndexPath addObject:indexPath];
}
// 刷新表格
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}