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;
    //注冊
    [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)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市毅弧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熄云,死亡現(xiàn)場離奇詭異激涤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)屡限,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炕倘,“玉大人钧大,你說我怎么就攤上這事≌中” “怎么了啊央?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵,是天一觀的道長涨醋。 經(jīng)常有香客問我瓜饥,道長,這世上最難降的妖魔是什么浴骂? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任乓土,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘趣苏。我一直安慰自己狡相,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布食磕。 她就那樣靜靜地躺著尽棕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪彬伦。 梳的紋絲不亂的頭發(fā)上滔悉,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天,我揣著相機(jī)與錄音单绑,去河邊找鬼回官。 笑死,一個胖子當(dāng)著我的面吹牛询张,可吹牛的內(nèi)容都是我干的孙乖。 我是一名探鬼主播,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼份氧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弯屈?” 一聲冷哼從身側(cè)響起蜗帜,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎资厉,沒想到半個月后厅缺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡宴偿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年湘捎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窄刘。...
    茶點(diǎn)故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡窥妇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出娩践,到底是詐尸還是另有隱情活翩,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布翻伺,位于F島的核電站材泄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏吨岭。R本人自食惡果不足惜拉宗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旦事,春花似錦魁巩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至单料,卻和暖如春埋凯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扫尖。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工白对, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人换怖。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓甩恼,卻偏偏與公主長得像,于是被迫代替她去往敵國和親沉颂。 傳聞我的和親對象是個殘疾皇子条摸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容