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;
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市殉疼,隨后出現(xiàn)的幾起案子梯浪,更是在濱河造成了極大的恐慌,老刑警劉巖瓢娜,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挂洛,死亡現(xiàn)場離奇詭異,居然都是意外死亡眠砾,警方通過查閱死者的電腦和手機虏劲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來褒颈,“玉大人柒巫,你說我怎么就攤上這事」韧瑁” “怎么了堡掏?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長刨疼。 經(jīng)常有香客問我泉唁,道長,這世上最難降的妖魔是什么揩慕? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任亭畜,我火速辦了婚禮,結(jié)果婚禮上迎卤,老公的妹妹穿的比我還像新娘拴鸵。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布宝踪。 她就那樣靜靜地躺著侨糟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瘩燥。 梳的紋絲不亂的頭發(fā)上秕重,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天,我揣著相機與錄音厉膀,去河邊找鬼溶耘。 笑死,一個胖子當著我的面吹牛服鹅,可吹牛的內(nèi)容都是我干的凳兵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼企软,長吁一口氣:“原來是場噩夢啊……” “哼庐扫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起仗哨,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤形庭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后厌漂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萨醒,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年苇倡,在試婚紗的時候發(fā)現(xiàn)自己被綠了富纸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡旨椒,死狀恐怖晓褪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情综慎,我是刑警寧澤辞州,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站寥粹,受9級特大地震影響变过,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜涝涤,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一媚狰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧阔拳,春花似錦崭孤、人聲如沸类嗤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遗锣。三九已至,卻和暖如春嗤形,著一層夾襖步出監(jiān)牢的瞬間精偿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工赋兵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笔咽,地道東北人。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓霹期,卻偏偏與公主長得像叶组,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子历造,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

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