ios系列开发之UITableView实现多行删除

实现多行删除的基本思路:
UITableViewCellEditingStyle返回的如果是UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert; 编辑表的时候就会出现多选按钮.我们可以将所有选中单元格对应的indexPath存放到一个可变数组中,然后点击删除按钮时移除数据中对应的元素.

#import "MainViewController.h"

@interface MainViewController ()
{
    UIButton * editButton;
    NSMutableArray * dataArray;
    NSMutableArray * selectedArray;
}
@end

@implementation MainViewController
- (void)dealloc{
    [dataArray release]; dataArray = nil;
    [_tableView release]; _tableView = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480- 44) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self setTableViewHeaderView];
    [self.view addSubview:_tableView];
    dataArray = [[NSMutableArray alloc]initWithObjects:@"大葫芦",@"大娃",@"二娃",@"三娃",@"四娃",@"五娃",@"六娃",@"七娃", nil];
    selectedArray = [[NSMutableArray alloc]init]; //非ARC模式不能使用静态方法创建全局变量
}

- (void)setTableViewHeaderView{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
    editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    editButton.frame = CGRectMake(120, 5, 80, 50);
    [editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [editButton setTitle:@"delete" forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(deleteSeletions:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:editButton];
    self.tableView.tableHeaderView = view;
}
#pragma mark - 删除按钮的监听事件
- (void)deleteSeletions:(UIButton *)button{
    NSString * title = self.tableView.editing ? @"delete" : @"ok";
    [button setTitle:title forState:UIControlStateNormal];
    self.tableView.editing = !self.tableView.editing;
    int count = selectedArray.count;
    if (count == 0) {
        return;
    }else {
        //冒泡排序  将indexPath.row 进行从小到大排序,避免删除数据时出现数组越界的BUG
        for (int i = 0 ; i < count - 1; i ++) {
            for (int j = 0 ; j < count - 1 - i; j++) {
                NSIndexPath * indexPath1 = [selectedArray objectAtIndex:j];
                NSIndexPath * indexPath2 = [selectedArray objectAtIndex:j+1];
                if (indexPath1.row > indexPath2.row) {
                    [selectedArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
            }
        }
        for (int i = count-1 ; i >= 0; i--) {  //从大到小删除对应的数据
            NSIndexPath * indexPath = [selectedArray objectAtIndex:i];
            [dataArray removeObjectAtIndex:indexPath.row];
        }
        [selectedArray removeAllObjects];
        [self.tableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (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) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];

    return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark - 选中单元格时方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([editButton.titleLabel.text isEqualToString:@"ok"]) {
        [selectedArray addObject:indexPath];
    }
}
#pragma mark - 取消选中单元格方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([editButton.titleLabel.text isEqualToString:@"ok"]) {
        [selectedArray removeObject:indexPath];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值