美文网首页
TableView多选(使用系统的编辑样式UITableView

TableView多选(使用系统的编辑样式UITableView

作者: 枫叶知秋 | 来源:发表于2018-07-31 16:05 被阅读453次

本人之前一直看到有关tableView多选的方式,

  1. 一种是用记录选中的cell放进数组,然后进行比对,进行多选和删除操作。
  2. 第二种是时用系统的tableView编辑样式UITableViewCellEditingStyle,这个中方式的好处:不用自己去记录选中的cell,和自己设置编辑多选样式。

参考文章:https://www.jianshu.com/p/a6e4cb42dd03(谢谢这位作者,文章写的很详细)

1.UITableViewCellEditingStyle编辑状态有三种样式

UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone 

2.设置编辑样式的代理方法

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

3.初始化 编辑 按钮,开启tableView多选功能


使用自带的编辑状态去做多选功能的好处

  • 不用去记录数据源中选中的数组,通过indexPathsForSelectedRows方法就可以获取到选中的cell,做删除操作时也会方便很多.
  • 进入退出编辑状态方便,还有自带的动画。ps:自定义cell时,子视图应加载在cell的contentView上。
  • 在要求不高的情况下,自带的图标就能满足需求。
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarItemClick:)];
self.navigationItem.rightBarButtonItem = item;

- (void)rightBarItemClick:(UIBarButtonItem *)item{
    if ([item.title isEqualToString:@"编辑"]) {
        if (self.listData.count == 0) {
            return;
        }
        item.title = @"取消";
        [self.wishTableView setEditing:YES animated:YES];
        [self showEitingView:YES];
    }else{
        item.title = @"编辑";
        [self.wishTableView setEditing:NO animated:YES];
        [self showEitingView:NO];
    }   
}
//  全选和删除按钮的编辑View
- (void)showEitingView:(BOOL)isShow{
    [self.editingView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(isShow?0:45);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
}

4. 全选、全不选、删除按钮处理

- (void)p__buttonClick:(UIButton *)sender{
    if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"删除"]) {
        //  删除选择的cell
        NSMutableIndexSet *insets = [[NSMutableIndexSet alloc] init];
        [[self.wishTableView indexPathsForSelectedRows] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [insets addIndex:obj.row];
        }];
        [self.listData removeObjectsAtIndexes:insets];
        [self.wishTableView deleteRowsAtIndexPaths:[self.wishTableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade];
    }else if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"全选"]) {
        [self.listData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //  全选
            [self.wishTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }];
        
        [sender setTitle:@"全不选" forState:UIControlStateNormal];
    }else if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"全不选"]){
        [self.wishTableView reloadData];
        [sender setTitle:@"全选" forState:UIControlStateNormal];
    }
}

5.点击的cell的方法进行处理

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 当时进行编辑时直接返回
    if (tableView.isEditing) {
        return;
    }
}

6.设置选中按钮的颜色

在cellForRowAtIndexPath方法中设置
cell.multipleSelectionBackgroundView = [UIView new];初始化 多个选择背景视图
cell.tintColor = [UIColor redColor];设置选中颜色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WishListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:wishCell];

//  初始化  多个选择背景视图
    cell.multipleSelectionBackgroundView = [UIView new];
//  设置选中颜色
    cell.tintColor = [UIColor redColor];

    return cell; 
}

7.自定义选中按钮的样式

在tableViewCell中进行设置

-(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:@"选中图片的名字"];
                    }else
                    {
                        img.image=[UIImage imageNamed:@"未选中图片名字"];
                    }
                }
            }
        }
    }
    [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:@"未选中图片名字"];
                    }
                }
            }
        }
    }
    
}

总结:

从这篇文章的作者那我学到一个全选方式

1.枚举器遍历

[self.listData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //  全选
            [self.wishTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }];

enumerateObjectsUsingBlock枚举器
枚举器是一种苹果官方推荐的更加面向对象的一种遍历方式,相比于for循环,它具有高度解耦、面向对象、使用方便等优势

想了解的小伙伴,可以直接在百度中搜索enumerateObjectsUsingBlock,我是之前没有了解到过,喷我可以,别喷的太狠就行

2.设置多选背景和选中按钮的颜色

在cellForRowAtIndexPath方法中设置
cell.multipleSelectionBackgroundView = [UIView new];初始化 多个选择背景视图
cell.tintColor = [UIColor redColor];设置选中颜色

3.设置多选按钮的自定义样式和图片

参考步骤7,使用 -(void)layoutSubviews{},方法进行重新布局
再使用- (void)setEditing:(BOOL)editing animated:(BOOL)animated设置第一图片为空的情况。

再次感谢作者:funnyS
参考网址:https://www.jianshu.com/p/5d4a8be9baf7

相关文章

网友评论

      本文标题:TableView多选(使用系统的编辑样式UITableView

      本文链接:https://www.haomeiwen.com/subject/ldkhvftx.html