最近做一款购物应用 写到了tableView的多选删除 修改系统删除左侧圆圈
添加两个数组属性 一为商品数组 二为选中的删除数组
@property (nonatomic,strong)NSMutableArray *goodsArray; //商品数组
@property (nonatomic,strong)NSMutableArray *selectArray; //选中数组
模拟数据
for (int i =0; i <4; i++) { ZSCarModel *model = [[ZSCarModelalloc]init]; model.image = [NSStringstringWithFormat:@"图%d.jpg",i]; model.goodsName = [NSStringstringWithFormat:@"第%d个商品",i + 1]; model.goodsPrice =@"¥123.00"; [self.goodsArrayaddObject:model]; } //设置tableView的编辑方式 [self.goodsTableViewsetEditing:YESanimated:YES];
设置tableView的编辑方式 系统默认蓝色圆圈
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { returnUITableViewCellEditingStyleDelete |UITableViewCellEditingStyleInsert; }
不要设置cell.selectionStyle为none
#pragma mark 选中行 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ZSCarModel *good = [_goodsArrayobjectAtIndex:indexPath.row]; if (![_selectArraycontainsObject:good]) { [_selectArray addObject:good]; } }
#pragma mark 取消选中行 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { ZSCarModel *good = [_goodsArrayobjectAtIndex:indexPath.row]; if ([_selectArraycontainsObject:good]) { [_selectArray removeObject:good]; } }
//删除 - (void)delete:(UIButton *)sender{ BOOL flag = self.goodsTableView.editing; if (flag) { if (_selectArray.count ==0) { [SVProgressHUDshowErrorWithStatus:@"请选择商品"]; return; } self.carBottom.selectAllBtn.selected =NO; NSMutableArray *indexArray = [NSMutableArrayarray]; for (ZSCarModel *goodin_selectArray) { NSInteger num = [_goodsArrayindexOfObject:good]; NSIndexPath *path = [NSIndexPathindexPathForRow:numinSection:0]; [indexArray addObject:path]; } [_goodsArray removeObjectsInArray:_selectArray]; [_selectArray removeAllObjects]; [_goodsTableViewdeleteRowsAtIndexPaths:indexArraywithRowAnimation:UITableViewRowAnimationFade]; } }
//全选 - (void)selectAllClick:(UIButton *)sender{ _goodsTableView.allowsMultipleSelectionDuringEditing =YES; sender.selected = !sender.selected; if (sender.selected) { for (int i =0; i <_goodsArray.count; i++) { NSIndexPath *indexPath = [NSIndexPathindexPathForItem:iinSection:0]; [_goodsTableViewselectRowAtIndexPath:indexPathanimated:NOscrollPosition:UITableViewScrollPositionBottom]; } [_selectArray addObjectsFromArray:_goodsArray]; }else{ for (int i =0; i <_goodsArray.count; i++) { NSIndexPath *indexPath = [NSIndexPathindexPathForItem:iinSection:0]; [_goodsTableViewdeselectRowAtIndexPath:indexPathanimated:NO]; } [_selectArray removeAllObjects]; } } //修改tableView编辑模式的默认删除圆圈 在自定义cell中重写父类方法 遍历出子视图的image并修改 - (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [supersetEditing:editinganimated:animated]; for (UIControl *controlinself.subviews){ if ([controlisMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){ for (UIView *vin control.subviews) { if ([visKindOfClass: [UIImageViewclass]]) { UIImageView *img=(UIImageView *)v; if (!self.selected) { img.image=[UIImageimageNamed:@"你的编辑模式下的图片"]; img.size =CGSizeMake(20,20); } } } } } } 重写父类点击方法 原理跟编辑模式修改图片相同 -(void)setSelected:(BOOL)selected{ [supersetSelected:selected]; }