tableview--編輯全選刪除

//
//  ViewController.m
//  tableViewDemo
//
//  Created by qianfeng on 16/11/16.
//  Copyright ? 2016年 qianfeng. All rights reserved.
//

#import "ViewController.h"
#import "Cell.h"

static NSString *cellID = @"Cell";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL isEditBtnSelected;
    BOOL isSelectBtnSelected;
}
@property(strong,nonatomic) UITableView *tableView;
@property(strong,nonatomic) NSMutableArray *dataSource;
@property(strong,nonatomic) UIButton *editBtn;
@property(strong,nonatomic) UIButton *selectBtn;
@end

@implementation ViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    isEditBtnSelected = NO;
    isSelectBtnSelected = NO;
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    //注冊(cè)
    [self.tableView registerNib:[UINib nibWithNibName:cellID bundle:nil] forCellReuseIdentifier:cellID];
    //編輯
    _editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_editBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _editBtn.frame = CGRectMake(0, 0, 80, 30);
    [_editBtn setTitle:@"編輯" forState:UIControlStateNormal];
    [_editBtn setTitle:@"取消編輯" forState:UIControlStateSelected];
    UIBarButtonItem *rightEditItem =  [[UIBarButtonItem alloc]initWithCustomView:_editBtn];
    //全選
    _selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _selectBtn.frame = CGRectMake(0, 0, 80, 30);
    [_selectBtn setTitle:@"全選" forState:UIControlStateNormal];
    [_selectBtn setTitle:@"取消全選" forState:UIControlStateSelected];
    UIBarButtonItem *rightSelectItem =  [[UIBarButtonItem alloc]initWithCustomView:_selectBtn];
    self.navigationItem.rightBarButtonItems = @[rightEditItem,rightSelectItem];
    //刪除
    UIBarButtonItem *deleteBtn =  [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnClick:)];
    self.navigationItem.leftBarButtonItem = deleteBtn;
    
    
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
    [self.dataSource addObject:@"xxx"];
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    return cell;
}
//編輯
-(void)editBtnClick:(id)sender
{
    isEditBtnSelected = !isEditBtnSelected;
    self.editBtn.selected = isEditBtnSelected;
    if (isEditBtnSelected) {
        self.tableView.editing = YES;
    }else{
        self.tableView.editing = NO;
    }
}
//全選
-(void)selectBtnClick:(id)sender
{
    isSelectBtnSelected = !isSelectBtnSelected;
    self.selectBtn.selected = isSelectBtnSelected;
    if (isSelectBtnSelected) {
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            
            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
            
            [self.tableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:0];
        }
    }else{
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            
            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
            
            [self.tableView deselectRowAtIndexPath:indexpath animated:YES];
        }
    }
}
// 刪除
-(void)deleteBtnClick:(id)sender
{
    //獲取所有
    NSArray *indexpaths = [self.tableView indexPathsForSelectedRows];
    
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    
    for (NSIndexPath *indexpath in indexpaths) {

        [indexSet addIndex:indexpath.row];
    }
    [self.dataSource removeObjectsAtIndexes:indexSet];
    
    [self.tableView deleteRowsAtIndexPaths:indexpaths withRowAnimation:UITableViewRowAnimationBottom];
}
@end

demo下載

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

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

  • 開發(fā)過程中或多或少都會(huì)遇到tableview的各種功能,這里簡(jiǎn)單記錄一下tableview的刪除和全選刪除功能,廢...
    去你的聯(lián)盟閱讀 13,411評(píng)論 7 25
  • 在以往的慣性思維中,對(duì)于批量刪除的操作一直以為是標(biāo)記被選中的Cell 控件進(jìn)行刪除,但往往前面被選中的Cell會(huì)被...
    黃曉堅(jiān)閱讀 2,616評(píng)論 0 1
  • 每當(dāng)太陽西沉,我坐在河邊破舊的碼頭上,遙望新澤西上方遼闊的天空,我感到似乎有未經(jīng)開墾的土地,所有的道路,所有的人都...
    在路上David閱讀 237評(píng)論 0 0
  • 我們主動(dòng)找對(duì)方說話的一方,往往叫做溝通的主動(dòng)方,主動(dòng)發(fā)起的一場(chǎng)溝通,我們就叫做“溝通邀請(qǐng)”。 任何希望和對(duì)方能夠建...
    兒童性教育映爾閱讀 1,489評(píng)論 1 12
  • rvm命令無法找到 一些有關(guān)網(wǎng)站http://blog.csdn.net/he520478/article/det...
    g0閱讀 215評(píng)論 0 0

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