数据刷新01全局刷新
数据刷新02局部刷新
左滑删除
滑动时自定义多个按钮
单选编辑模式
是什么?
![](https://img.haomeiwen.com/i2337348/1f9b126534c0428d.gif)
示例代码
- (void)viewDidLoad {
[super viewDidLoad];
// 单选-编辑模式下,可以实现多选.🔘
//[self.tableView setAllowsMultipleSelectionDuringEditing:YES];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
// 开始时,隐藏"删除按钮"
self.delBtn.hidden = YES;
}
- (IBAction)editBtn{
//进入编辑模式,⛔️
[self.tableView setEditing:!self.tableView.editing animated:YES];
//显示"删除按钮"
self.delBtn.hidden = !self.tableView.editing;
}
注意: 不能一边遍历,一边删除。
因为每删除一个元素,索引都会发生变化
- (IBAction)removeBtn{
NSArray<NSIndexPath *> *indexPath = self.tableView.indexPathsForSelectedRows; // 获取选中的索引号(path: 列号 - 行号);
NSMutableArray * muArray = [NSMutableArray array];
for (NSIndexPath * arrayTemp in indexPath) {//根据选择的索引号,获取对应对象,放进新的集合里面
//注意,不能一边遍历,一边删除。因为每删除一个元素,索引都会发生变化
//[self.wineData removeObjectAtIndex:arrayTemp.row];
Wine * wine = self.wineData[arrayTemp.row];
[muArray addObject:wine];
}
//在数组里面,删除指定数组的数据
[self.wineData removeObjectsInArray:muArray];
//刷新数据列表
//[self.tableView reloadData]; //全局刷新
[self.tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationTop];//局部刷新
}
网友评论