UITableView的多选删除cell

2017-01-19  本文已影响0人  怒气男孩

UITableView是iOS语言里非常重要的类来在table中展示数据。如:


这些界面都可以用UITableView来显示。
有些时候我们需要对这些表格视图进行编辑,也就是删除或增加,这就需要调用到UITableView的delegate和dataSource协议里的一些方法了,我们要实现对表格视图的编辑,需要调用的方法有:
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) UIButton *selectedBtn;//全选按钮
@property (weak, nonatomic) UIButton *deleteBtn;//删除按钮
@property (strong, nonatomic) NSMutableArray *dataArr;//数据数组
@property (strong, nonatomic) NSMutableArray *deleteArr;//要删除的cell放到这组
@end

- (void)viewDidLoad {
    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;
}
//比较基础的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//表格区的数量
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//每个区的cell行数
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//每行cell的内容设置
}

以上的方法是比较基础的,而要实现编辑,就要调用到一些特殊的方法:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//返回tableView编辑时的形式
//UITableViewCellEditingStyleDelete:删除
//UITableViewCellEditingStyleInsert:添加
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

如果单独返回UITableViewCellEditingStyleDelete时,表格视图为


点击Delete就删除那一行cell。
如果返回UITableViewCellEditingStyleInsert时,表格视图为

但是,两个方式一起返回事就变成cell前出现一个圆形按钮,点击cell后会选择,实现cell的多选


后面的代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//当点击一行cell时执行
//将点击了的那行cell的下标与数据数组里的相同内容取出来放入deleteArr数组里
[self.deleteArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
//再次点击选中的cell时就取消选择了,这时这些这个方法
//将对应的cell内容从删除数组里移除
[self.deleteArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
}

这时就可以实现多选要删除的目标,剩下的就是删除事件。删除按钮deleteBtn的属性设置各凭喜好,关键是点击事件,代码为:

- (void)deleteBtnClick {
    if (self.tableView.editing) {
        //删除
        [self.dataArr removeObjectsInArray:self.deleteArr];
        // 刷新
        [self.tableView reloadData];
        //清空删除队列
    }
}

如果用户要选择全部数据,全选按钮selectedBtn的点击事件代码如下:

- (void)selectedBtnClick {
    if (!self.selectedBtn.selected) {
        self.selectedBtn.selected = YES;
        //先清空删除队列里的数据,以免后面数据重复
        [self.deleteArr removeAllObjects];
        for (int i = 0; i < self.dataArr.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            //遍历数据数组,将所有对应cell都变成选中状态
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
}
         //将整个数据源数组都放入删除队列里
        [self.deleteArr addObjectsFromArray:self.dataArr];
    }else{
        //再次点击全选按钮时,变为全不选
        self.selectedBtn.selected = NO;
        [self.deleteArr removeAllObjects];
        for (int i = 0; i < self.dataArr.count; i++) {
            //遍历数据数组,将所有对应cell都变成未选中状态
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
        }
}

以上方法就可以实现UITableView对cell的多选删除了。

上一篇 下一篇

猜你喜欢

热点阅读