美文网首页iOS Developer移动开发iOS 开发
iOS TableView多选删除,全选删除

iOS TableView多选删除,全选删除

作者: woooooo | 来源:发表于2016-09-06 13:16 被阅读697次
    11.gif

    代码比较简单,一般都能看懂

    界面创建

    #import "RootViewController.h"
    static NSString *const cellIdentifier = @"cell";
    
    @interface RootViewController ()
    <
    UITableViewDelegate,
    UITableViewDataSource
    >
    //定义内部属性
    @property(nonatomic, retain)UITableView *tableView;
    @property(nonatomic, retain)NSMutableArray *dataSourceArray;
    @property(nonatomic, retain)NSMutableArray *deleteArray;
    @property(nonatomic, retain)UIButton *selectButton;
    @property(nonatomic, retain)UIButton *selectAllButton;
    @property(nonatomic, retain)UIButton *deleteButton;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //获取资源文件
        NSString *peopleNamePath = [[NSBundle mainBundle] pathForResource:@"PeopleName" ofType:@"plist"];
        self.dataSourceArray = [NSMutableArray arrayWithContentsOfFile:peopleNamePath];
        
        self.deleteArray = [NSMutableArray array];
        
        //初始化tableView
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80;
        [self.view addSubview:_tableView];
        [_tableView release];
        [_tableView
        //注册cell
        registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        
        //右侧选择按钮
        self.selectButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _selectButton.frame = CGRectMake(0, 0, 60, 30);
        [_selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_selectButton setTitle:@"选择" forState:UIControlStateNormal];
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_selectButton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
        
        //下方删除按钮
        self.deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _deleteButton.frame = CGRectMake(0, self.view.bounds.size.height - 60, self.view.bounds.size.width, 60);
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteButton.backgroundColor = [UIColor redColor];
        [self.view addSubview:_deleteButton];
        [_deleteButton release];
        _deleteButton.hidden = YES;
        
        
        //左侧全选按钮
        self.selectAllButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _selectAllButton.frame = CGRectMake(0, 0, 60, 30);
        [_selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
        [_selectAllButton addTarget:self action:@selector(selectAllButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
        [leftBarButtonItem release];
        [_selectAllButton release];
        //默认隐藏  再点击选择按钮后再显示
        _selectAllButton.hidden = YES;
        
        
        
    }
    
    • 全选按钮响应事件
    - (void)selectAllButtonAction:(UIButton *)selectAllButton {
        
        for (int i = 0; i < _dataSourceArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
            [_deleteArray addObjectsFromArray:_dataSourceArray];
        }
        
    }
    
    • 删除按钮响应事件
    - (void)deleteButtonAction:(UIButton *)deleteButton  {
        
        if ([_tableView isEditing]) {
            [self.dataSourceArray removeObjectsInArray:_deleteArray];
            [_tableView reloadData];
        }
        return;
    
        
    }
    
    • 对tableView已经被选中的row,添加到删除数组中
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        NSLog(@"%@",_dataSourceArray[indexPath.row]);
        [_deleteArray addObject:_dataSourceArray[indexPath.row]];
        
    }
    
    • select按钮响应事件
    - (void)selectButtonAction:(UIButton *)selectButton {
        
        _tableView.allowsMultipleSelectionDuringEditing = YES;
        _tableView.editing = !_tableView.editing;
        
        if (_tableView.editing) {
            _deleteButton.hidden = NO;
            _selectAllButton.hidden = NO;
            [selectButton setTitle:@"完成" forState:UIControlStateNormal];
        
        }
        else {
            _selectAllButton.hidden = YES;
            _deleteButton.hidden = YES;
            [selectButton setTitle:@"选择" forState:UIControlStateNormal];
        }
        
    }
    
    • tableView代理方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _dataSourceArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.textLabel.text = _dataSourceArray[indexPath.row];
        return cell;
    }
    

    相关文章

      网友评论

        本文标题:iOS TableView多选删除,全选删除

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