UITableView 多选删除功能 IOS

UITableView多选状态 删除

效果图
这里写图片描述

首先在UITableView的 edittingStyleForRowAtIndexPath函数中,添加如下代码

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

—-初始时设置TableView setEditing=YES;

具体的代码实现和效果图如下:

#import "DuoxuanTableView.h"

@interface DuoxuanTableView ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton * button11;
    NSMutableArray * selectedDic;
    NSMutableArray * dataArray;
}
@end

@implementation DuoxuanTableView
@synthesize duoxianTable;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    // Do any additional setup after loading the view.
    [self initDuoxuanTable];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - tableView代理方法

- (void)initDuoxuanTable {

    button11 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    button11.backgroundColor = [UIColor orangeColor];
    [button11 addTarget:self action:@selector(rightBtnPressedWithSure:) forControlEvents:UIControlEventTouchUpInside];
    [button11 setTitle:@"删除" forState:UIControlStateNormal];
    [self.view addSubview:button11];

    duoxianTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-100)];
    duoxianTable.delegate = self;
    duoxianTable.dataSource = self;
    [self.view addSubview:duoxianTable];
//    [duoxianTable setEditing:YES animated:YES];


    // Do any additional setup after loading the view, typically from a nib.
    dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
    selectedDic = [[NSMutableArray alloc] init];
}

- (void)rightBtnPressedWithSure:(UIButton *)button {

    if ([button11.titleLabel.text isEqual:@"删除"]) {

        NSLog(@"现在点击的是要选手编辑状态");
        [button11 setTitle:@"确定" forState:UIControlStateNormal];
        [duoxianTable setEditing:YES animated:YES];

    } else if([button11.titleLabel.text isEqual:@"确定"]) {

        NSLog(@"现在点击的是删除后  正常状态");

        int count = (int)[selectedDic count];
        if(count > 0 ) {
            for (int i = 0; i < count; i ++) {
                NSInteger row = [[ selectedDic objectAtIndex:i] row];
                [dataArray removeObjectAtIndex:row];
            }

            [duoxianTable deleteRowsAtIndexPaths:selectedDic withRowAnimation:UITableViewRowAnimationFade];
            [selectedDic removeAllObjects];

            [duoxianTable setEditing:NO animated:YES];


            [button11 setTitle:@"删除" forState:UIControlStateNormal];
            [duoxianTable setEditing:NO animated:YES];

        }else {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
            [alert show];

        }


    }

}


#pragma -mark
#pragma tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  [dataArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

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

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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([button11.titleLabel.text isEqualToString:@"确定"]) {
        [selectedDic addObject:indexPath];
            NSLog(@"Select---->:%@",selectedDic);
    }
}

//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([button11.titleLabel.text isEqualToString:@"确定"]) {
        [selectedDic removeObject:indexPath];
               NSLog(@"Deselect---->:%@",selectedDic);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableViewIdentifier = @"TableViewIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
    NSInteger row = [indexPath row];
    cell.textLabel.text = [ dataArray objectAtIndex:row];
    return cell;
}
//#pragma mark-
#pragma AlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [button11 setTitle:@"删除" forState:UIControlStateNormal];
        [duoxianTable setEditing:NO animated:YES];
    }
}

@end

效果图如下:
这里写图片描述

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值