iOS TableView多選刪除,全選刪除

11.gif

代碼比較簡單,一般都能看懂

界面創(chuàng)建

#import "RootViewController.h"
static NSString *const cellIdentifier = @"cell";

@interface RootViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
//定義內(nèi)部屬性
@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];
    
    //右側(cè)選擇按鈕
    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;
    
    
    //左側(cè)全選按鈕
    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已經(jīng)被選中的row,添加到刪除數(shù)組中
- (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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容