编辑模式
- (IBAction)remove {
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
self.tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.wineArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return @[action1,action];
}
批量删除
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.deletedButton.hidden = YES;
}
- (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 deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
自定义批量删除
- 在模型中增加一个BOOL类型的checked属性
- 在控制器.m方法中增加一个类扩展,可变数组seletedIndexPath,用于记录用户选中的索引
- 在代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath进行判断
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGWine *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];
}
- (IBAction)remove {
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];
}