编辑模式

- (IBAction)remove {
    // 进入编辑模式左边出现一排减号按钮
//    self.tableView.editing = !self.tableView.isEditing;
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    self.tableView.editing = YES;
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        [self.tableView reloadData];
//        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        // 退出编辑模式
        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];

    // 告诉tableView在编辑模式下可以多选
    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 reloadData];
    [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];
}

results matching ""

    No results matching ""