美文网首页集思广益iOS开发专区临时收藏
iOS 编辑tableView的cell多选(全选)删除功能

iOS 编辑tableView的cell多选(全选)删除功能

作者: IOSMan | 来源:发表于2016-08-14 21:00 被阅读7472次

本期带来 tableview 的编辑模式,多选删除、全选删除,统计选中删除数功能。

![Snip20160814_2.png](http:https://img.haomeiwen.com/i1737720/0e07c6f843f478af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

实现下面代理方法可以调用系统的选择 cell 左边的蓝色选中图标,当然必须是tableView.editing = YES;

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

删除 cell 用一个中介数组变量来进行统计要删除的 cell
选择 cell 用方法
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
注意要用UITableViewScrollPositionBottom,这样才能从头选到尾,不会出现只选择屏幕显示的区域

具体代码


#import "ViewController.h"
#import "Masonry.h"
#import "UIButton+Extention.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) UIButton *selectedBtn;
@property (weak, nonatomic) UIButton *deleteBtn;
@property (weak, nonatomic) UIImageView *editView;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSMutableArray *deleteArr;
@property (assign, nonatomic) NSInteger deleteNum;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.deleteArr = [[NSMutableArray alloc]init];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 70)];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.editing = YES;
    
    //    编辑区域
    UIImageView *editView = [[UIImageView alloc]init];
    [self.view addSubview:editView];
    self.editView = editView;
    
    editView.userInteractionEnabled = YES;
    //    editView.hidden = YES;
    editView.image = [UIImage imageNamed:@"MyLivingBtnBackground.png"];
    
    
    [editView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@65);
    }];
    
    
    
    //    全选
    UIButton *selectedBtn = [UIButton buttonWithImage:@"AllSelectedBtn" title:@"全选" target:self action:@selector(selectedBtnClick)];
    [editView addSubview:selectedBtn];
    self.selectedBtn = selectedBtn;
    
    [selectedBtn setTitle:@"取消全选" forState:UIControlStateSelected];
    
    [selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 45));
        make.centerY.equalTo(editView);
        make.centerX.equalTo(@-80);
    }];
    
    //    删除
    UIButton *deleteBtn = [UIButton buttonWithImage:@"delete_btn" title:@"删除(0)" target:self action:@selector(deleteBtnClick)];
    [editView addSubview:deleteBtn];
    self.deleteBtn = deleteBtn;
    
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 45));
        make.centerY.equalTo(editView);
        make.centerX.equalTo(@80);
    }];
}
#pragma mark - 全选按钮被点击
- (void)selectedBtnClick {
    if (!self.selectedBtn.selected) {
        self.selectedBtn.selected = YES;
        
        for (int i = 0; i < self.dataArr.count; i++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
        }
        [self.deleteArr addObjectsFromArray:self.dataArr];
        self.deleteNum = self.dataArr.count;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"删除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    }else{
        self.selectedBtn.selected = NO;
        [self.deleteArr removeAllObjects];
        for (int i = 0; i < self.dataArr.count; i++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
            //            cell.selected = NO;
        }
        self.deleteNum = 0;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"删除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    }
}

#pragma mark - 删除按钮
- (void)deleteBtnClick {
    if (self.tableView.editing) {
        //删除

        [self.dataArr removeObjectsInArray:self.deleteArr];
        [self.tableView reloadData];
        
        self.deleteNum = 0;
        [self.deleteBtn setTitle:[NSString stringWithFormat:@"删除(%lu)",self.deleteNum] forState:UIControlStateNormal];
        
        self.selectedBtn.selected = NO;
        //            你的网络请求
        
        
    }
}


#pragma mark - DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row]];

    return cell;
}

#pragma mark - tableViewDelegate

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.deleteArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
    self.deleteNum += 1;
    [self.deleteBtn setTitle:[NSString stringWithFormat:@"删除(%lu)",self.deleteNum] forState:UIControlStateNormal];
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.deleteArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
    self.deleteNum -= 1;
    [self.deleteBtn setTitle:[NSString stringWithFormat:@"删除(%lu)",self.deleteNum] forState:UIControlStateNormal];
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
        for (int i = 0; i < 100; ++i) {
            [_dataArr addObject:@(i)];
        }
    }
    return _dataArr;
}


@end

如果不想用系统自带的蓝色圆圈选中样式,想要修改为自定义的选中样式,可以看我的这篇博文http://www.jianshu.com/p/678da944f6fb

gitHub 地址:https://github.com/D-james/MultipleSelectCell

相关文章

网友评论

  • 2f766dce1076:我用的自定义cell. 编辑模式下,多选框出来了.但是cell不往右移动..二者重叠在一起了...怎么办?
  • 简简728:可以既可以选中又可以跳转吗
    Cocoa_Coder:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

    if (tableView.isEditing) {
    return;//选中
    }
    //不在编辑状态下 可以跳转



    }
  • 洁简:左滑按钮的大小可以改变吗
    IOSMan:@洁简 系统的不行,你可以不用系统的,自定义一个,但是麻烦
  • 939a9e96657a:明天用
  • 小宋的简单生活:全选之后取消全选,点击删除还是将全部删除了,应该做个处理
    IOSMan:@小宋的简单生活 谢谢指正,的确有这个问题 :grin:
  • 嗨_我是大鑫啊:我想点击圆点的时候才可以选择。然后可以删除,点击cell的时候我想展示cell的内容。。咋搞??楼主,,在线等啊!!!
    嗨_我是大鑫啊:@IOSMan 谢谢了
    嗨_我是大鑫啊:@IOSMan 哦。。可能和我们的业务要求不符了
    IOSMan:@BigXin 如果按照你这个思路你的圆点需要自定义,不能用系统的了,这样比较麻烦。但是你可以换一种思路,加一个编辑按钮,在编辑模式下点击cell是选择cell,但是退出编辑模式,点击cell,是进入cell的内容页面,你看看淘宝,京东的购物车都是这么设计的
  • 棍武中原:好的,谢谢楼主
  • 棍武中原:楼主,我想有个按钮一次全部选中有没有什么方法
    IOSMan:@棍武中原 我这代码里有全选按钮啊,你去 github 下载原码看看
  • func_老衲姓罗:indexPathForRow这个API找不到啊
    func_老衲姓罗:@IOSMan let indexPath1=NSIndexPath(forRow: i,inSection: 0)swift 中是这么写的,因为我command进去没有发现这个属性,后来看别人是这么写的
    IOSMan:@func_老衲姓罗 可能是因为你没有设置代理,你下我代码看看
  • d05f1c60e82b:我不知道怎样控制左边出现的是红色的减号还是绿色的圆圈
    IOSMan:@Civel_Xu 蓝色选中圆圈是天生的,系统自带的
    Civel_Xu:蓝色圈 自定义 ?
    IOSMan:@臂化羽 如果你只是进入tableView.editing = YES编辑模式就是红色减号,加上代理方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }就是蓝色圆圈
  • 634585edc862:楼主你好,我现在点击cell的时候会直接调用cell被点击的代理方法push到另一个界面,无法选择cell怎么解决啊
    IOSMan:@子瓜虫 当进去编辑模式,在点击cell跳转那加判断,不是编辑模式才可以跳转

本文标题:iOS 编辑tableView的cell多选(全选)删除功能

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