UITableView多選刪除操作

1.前言

近日碰到了UITableView的多選刪除操作,雖然不是太復(fù)雜,但是還是做個記錄吧,以后再遇到可以直接使用了,同時也希望能對有需要的人帶來幫助。

2.效果展示

包含功能有:全選,全不選,多個刪除,單個左滑刪除


未命名.gif

3.代碼及注釋

#import "SelectAndDeleteVC.h"

@interface SelectAndDeleteVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
//底部刪除View
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *selectButton;
@end

@implementation SelectAndDeleteVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [[NSMutableArray alloc] initWithArray:@[@"測試數(shù)據(jù)1",@"測試數(shù)據(jù)2",@"測試數(shù)據(jù)3"]];
    
    [self drawView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)drawView{
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //在編輯時可以多選,開啟后就顯示圓形選擇框
    _tableView.allowsMultipleSelectionDuringEditing = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    [self.view addSubview:self.bottomView];
}

- (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 *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = _dataArray[indexPath.row];
    
    return cell;
}


- (UIView *)bottomView{
    if (!_bottomView){
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight , ScreenWidth, 50*UIRate)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        UIButton *selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth/2.0, 50*UIRate)];
        [selectButton setTitle:@"全選" forState:UIControlStateNormal];
        [selectButton setBackgroundColor:[UIColor grayColor]];
        [selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:selectButton];
        
        UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2.0, 0, ScreenWidth/2.0, 50*UIRate)];
        [deleteButton setTitle:@"刪除" forState:UIControlStateNormal];
        [deleteButton setBackgroundColor:[UIColor redColor]];
        [deleteButton addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:deleteButton];
        
    }
    return _bottomView;
}

//走了左滑刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"走了左滑刪除%ld", (long)indexPath.row);
    if (editingStyle == UITableViewCellEditingStyleDelete){//刪除操作
        [self.dataArray removeObjectAtIndex:indexPath.row];
        
        //刪除某行并配有動畫
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//更改左滑后的字體顯示
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果系統(tǒng)是英文,會顯示delete,這里可以改成自己想顯示的內(nèi)容
    return @"刪除";
}

//全選
- (void)selectButtonAction:(UIButton *)button{
    
    if ([button.currentTitle isEqualToString:@"全選"]){
        //遍歷循環(huán)
        [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //全選
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        }];
        
        [button setTitle:@"全不選" forState:UIControlStateNormal];
    }else {
        //刷新數(shù)據(jù)即可實現(xiàn)全不選狀態(tài)
        [self.tableView reloadData];
        [button setTitle:@"全選" forState:UIControlStateNormal];
    }
}

//刪除
- (void)deleteBtnAction:(UIButton *)button{
    
    //indexPathsForSelectedRows 可以獲得選中的行
    NSArray *array = self.tableView.indexPathsForSelectedRows;
    if (!array.count){
        [LCProgressHUD showMessage:@"請選擇要刪除的列表"];
        return;
    }
    
    NSMutableArray *selectArray = [[NSMutableArray alloc] init];
    
    for (NSIndexPath *indexPath in array){
        //取出數(shù)據(jù)
        [selectArray addObject:self.dataArray[indexPath.row]];
    }
    //刪除數(shù)據(jù)
    [self.dataArray removeObjectsInArray:selectArray];
    //刪除行
    [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark 右編輯
- (void)rightItemAction:(UIBarButtonItem *)item
{
    if (!self.tableView.isEditing){
        item.title = @"取消";
        //伴隨編輯動畫
        [self.tableView setEditing:YES animated:YES];
        
        [UIView animateWithDuration:0.5 animations:^{
            self.bottomView.frame = CGRectMake(0, ScreenHeight - 50, ScreenWidth, 50);
        }];
        
    }else {
        item.title = @"編輯";
        [self.tableView setEditing:NO animated:YES];
        [UIView animateWithDuration:0.5 animations:^{
            self.bottomView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 50);
        }];
    }
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,216評論 4 61
  • 玉蘭花對焦在枝干上了,硬朗的線條在朦朧的花朵印襯下,我竟然覺得有些好看。
    孤獨的致郁者閱讀 487評論 6 3
  • 有時候,連我自己都分不清,我是在夢里還是在現(xiàn)實。 我也不知道當再遇到雙面事件時,我的心更加傾向于的是積極的還是消極...
    夏暖心閱讀 157評論 0 0
  • 當壓力來臨的時候 這是一個喧囂的世界,一個焦慮的時代,一個人人都喊累,行行都說煩的社會。不管...
    情深深愛萌萌閱讀 658評論 1 2

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