1、系统方式
- 将要删除的数据添加到待删数组中,从数据源中删除待删数组中包含的数据,刷新表格。
- OC 中可设置编辑模式为 UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; 或者设置 tableView.allowsMultipleSelectionDuringEditing = YES; 进入多选模式。
-
1.1 待删数据数组初始化
// 声明待删数据数组
@property(nonatomic, retain)NSMutableArray *tempDeleteArray;
// 初始化待删数据数组
tempDeleteArray = [[NSMutableArray alloc] init];
// 编辑按钮点击响应事件
- (void)editClick:(UIButton *)button {
// 改变编辑开关状态
[tableView setEditing:!tableView.editing animated:YES];
// 设置编辑模式,允许编辑时多选,或者在协议方法中设置
tableView.allowsMultipleSelectionDuringEditing = YES;
// 当编辑状态发生改变的时候,清空待删数组
[tempDeleteArray removeAllObjects];
[tableView reloadData];
}
// 删除按钮点击响应事件
- (void)deleteClick:(UIButton *)button {
// 从数据源中删除待选数组中包含的数据
[myDataArray removeObjectsInArray:tempDeleteArray];
// 清空待删数组
[tempDeleteArray removeAllObjects];
[tableView reloadData];
}
// 设置编辑模式
/*
删除、插入、多选删除,不设置默认时为删除,
或者在编辑按钮点击事件中直接设置 tableView.allowsMultipleSelectionDuringEditing = YES;
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// 多选删除
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
// 表格选中点击响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 判断 tableView 的编辑状态,表格处于编辑状态
if (tableView.isEditing) {
// 选中 cell 的时候,将对应的数据源模型添加到待删除数组中
[tempDeleteArray addObject:[myDataArray objectAtIndex:indexPath.row]];
}
else {
// 恢复未选中状态时的颜色
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
// 表格取消选中点击响应事件
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
// 判断 tableView 的编辑状态,表格处于编辑状态
if (tableView.isEditing) {
// 将对应的数据模型从待删除数组中移除
[tempDeleteArray removeObject:[myDataArray objectAtIndex:indexPath.row]];
}
}
-
1.4 运行效果
- __