一些列表经常需要编辑多选的功能,而UITableview自带多选删除的功能,使用起来方便,不需要自己去做数据存储和选中状态转换,可以减少不少开发时间。下面就来介绍下UITableView多选的使用。 效果 :
编辑状态UITableViewCellEditingStyle有三种模式:
UITableViewCellEditingStyleDeleteUITableViewCellEditingStyleInsertUITableViewCellEditingStyleNone多选框的风格, 只需要风格同时包含UITableViewCellEditingStyleDelete和UITableViewCellEditingStyleInsert就可以了
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; }然后设置UITableview的Editing状态,就可开启tableview的多选功能。
还可以修改有点击选中图标的颜色,其他默认图片的颜色也会因此而修改,
self.tintColor = [UIColor redColor];效果:
如果不想使用默认图标的话,也可以在自定义:
-(void)layoutSubviews { for (UIControl *control in self.subviews){ if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){ for (UIView *v in control.subviews) { if ([v isKindOfClass: [UIImageView class]]) { UIImageView *img=(UIImageView *)v; if (self.selected) { img.image=[UIImage imageNamed:@"xuanzhong_icon"]; }else { img.image=[UIImage imageNamed:@"weixuanzhong_icon"]; } } } } } [super layoutSubviews]; } //适配第一次图片为空的情况 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; for (UIControl *control in self.subviews){ if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){ for (UIView *v in control.subviews) { if ([v isKindOfClass: [UIImageView class]]) { UIImageView *img=(UIImageView *)v; if (!self.selected) { img.image=[UIImage imageNamed:@"weixuanzhong_icon"]; } } } } } }效果:
demo地址:https://github.com/yxsufaniOS/TableViewMutableSelectDemo
链接:https://www.jianshu.com/p/a6e4cb42dd03