iOS 自定义cell多选删除

编辑模式设定
1
2
3
4
5
6
7
/**
* tableView 是否可以进入编辑状态
*
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
1
2
3
4
5
6
7
8
/**
* 设置编辑样式(如果不设置,会使用系统自带的删除方法,不能多选删除,只能单个删除. 会调用下面 注释掉的那个方法)
*
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
cell选中样式替换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 选中Cell的时候调用
*
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
ShopCouponsModel *model = [self.dataSource objectAtIndex:indexPath.row];
//点击Cell时更改系统自带的选中视图
if (tableView.editing == YES) {
UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];
imageView.image = [UIImage imageNamed:@"shopCar_selectBtn_selected"];
[self.selectArray addObject:model];
[self.selectOkView refreshSelectOkViewWith:self.selectArray orderPrice:self.orderPrice];
}
}
/**
* 取消选中时调用
*
*/
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
ShopCouponsModel *model = [self.dataSource objectAtIndex:indexPath.row];
if (tableView.editing == YES) {
UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];
imageView.image = [UIImage imageNamed:@"shopCar_selectBtn_normal"];
[self.selectArray removeObject:model];
[self.selectOkView refreshSelectOkViewWith:self.selectArray orderPrice:self.orderPrice];
}
}
另外还需加上以下代码 防止cell复用时候错乱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ShopCouponsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shopSelectedCouponCell forIndexPath:indexPath];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:@"ShopCouponsViewCell" owner:nil options:nil].lastObject;
}
//去除了点击色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
ShopCouponsModel *model = [self.dataSource objectAtIndex:indexPath.row];
[cell refreshWith:model];
for (ShopCouponsModel *selectModel in self.selectArray) {
if ([model.ID isEqualToString:selectModel.ID]) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
/**
* 复用过程中,自定义的选中按钮不会被系统还原,需配合上述 forIndexPath: 方法(需在 Cell选中状态 和 TableView编辑状态下)
*/
if (cell.selected == YES && tableView.editing == YES) {
/**
* 取出系统自带的选中视图(在TableView允许编辑状态下打印 cell.subviews, 其中UITableViewCellEditControl 即为编辑状态下的视图
[[cell.subviews lastObject]subviews] 选中状态下的视图)
*/
UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];
/**
* 改变选中状态下的视图
*/
imageView.image = [UIImage imageNamed:@"shopCar_selectBtn_selected"];
}else if (cell.selected == NO && tableView.editing == YES){
UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];
/**
* 还原未选中状态
*/
imageView.image = [UIImage imageNamed:@"shopCar_selectBtn_normal"];
}
return cell;
}
做完这些单选完成 以下是点击全选时候的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//全选
- (void)allSelectCellWith:(UIButton *)button
{
if (button.selected == NO) {
[self.tableView reloadData];
[self.deleteDataArray removeAllObjects];
return;
}
for (int i = 0; i < self.dataSource.count; i++) {
ShopDayBrowHistoryModel *model = self.dataSource[i];
for (int j = 0; j < model.productBroHistoryList.count; j++) {
if (![self.deleteDataArray containsObject:model.productBroHistoryList[j]]) {
[self.deleteDataArray addObject:model.productBroHistoryList[j]];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
//由于全选用的selectRowAtIndexPath方法 没走didSelectRowAtIndexPath方法 所以需要手动替换选中图片
NSArray *subviews = [[self.tableView cellForRowAtIndexPath:indexPath] subviews];
for (id subCell in subviews) {
if ([subCell isKindOfClass:[UIControl class]]) {
for (UIImageView *circleImage in [subCell subviews]) {
circleImage.image = [UIImage imageNamed:@"shopCar_selectBtn_selected"];
}
}
}
}
}
}