UITableView删除多行

18 篇文章 0 订阅


主要是-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  这个方法,返回1是删除,返回2是插入,3是复选框。


直接来代码吧:


#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSMutableArray *_arrayForDelete;
    NSMutableArray *_indexPathArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareData];
    [self uiConfig];
    
}

-(void)prepareData
{
    _indexPathArray = [[NSMutableArray alloc]init];
    _arrayForDelete = [[NSMutableArray alloc]init];
    _dataArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<20; i++) {
        [_dataArray addObject:[NSString stringWithFormat:@"%d行",i]];
    }
    
}

-(void)uiConfig
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

//点击右上角edit按钮的时候调用
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing == NO) {
        [_dataArray removeObjectsInArray:_arrayForDelete];
//        [_tableView reloadData];//刷新
        [_arrayForDelete removeAllObjects];
        [_tableView deleteRowsAtIndexPaths:_indexPathArray withRowAnimation:UITableViewRowAnimationFade];
        [_indexPathArray removeAllObjects];
    }
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.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 = _dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

//选择的时候调用,将需要删除的cell的数据源和索引存起来
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_arrayForDelete addObject:_dataArray[indexPath.row]];
    [_indexPathArray addObject:indexPath];
}

//当反选的时候调用,就是取消选择的时候调用
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_arrayForDelete removeObject:_dataArray[indexPath.row]];
    [_indexPathArray removeObject:indexPath];
}

@end

效果:

点击Edit:

选择:

点击Done:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值