UITableView 多選、全選、全部取消

iOS開發(fā) ?tableView多選、全選的簡(jiǎn)單使用


以下兩張圖為全部代碼(圖片看著方便些,后面有代碼可供復(fù)制參考)


全部代碼

#import "ViewController.h"@interface ViewController ()@property(nonatomic,retain)UITableView *tableView;

@property(nonatomic,retain)NSMutableArray *dataSource;

@property(nonatomic,assign)BOOL isAllSelected;

@property(nonatomic,retain)NSMutableArray *selectData;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

for (int i=0; i<20; i++) {

[self.dataSource addObject:@(i)];

}

self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];

self.tableView.editing = YES;

self.tableView.delegate = self;

self.tableView.dataSource = self;

[self.view addSubview:self.tableView];

}

- (IBAction)printObjs:(UIBarButtonItem *)sender {

NSLog(@"%@",self.selectData);//打印當(dāng)前選中的內(nèi)容

NSLog(@"%@",[self.tableView indexPathsForSelectedRows]);//也可以根據(jù)當(dāng)前選中的行號(hào)。對(duì)selectData進(jìn)行賦值。建議使用該種方式。這種方式不需要在點(diǎn)擊cell時(shí)去對(duì)selectData操作。只在需要時(shí)進(jìn)行一次賦值即可。

}

- (IBAction)selectAll:(id)sender {

if (_isAllSelected == NO) {

_isAllSelected = YES;

[sender setTitle:@"取消"];

for (int i = 0; i < self.dataSource.count; i++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

}

[self.selectData addObjectsFromArray:self.dataSource];

} else {

_isAllSelected = NO;

[sender setTitle:@"全選"];

for (int i = 0; i < self.dataSource.count; i++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

}

[self.selectData removeAllObjects];

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.dataSource.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row]];

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.selectData addObject:self.dataSource[indexPath.row]];

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.selectData removeObject:self.dataSource[indexPath.row]];

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;

}

-(NSMutableArray *)dataSource{

if (!_dataSource) {

_dataSource = [[NSMutableArray alloc] initWithCapacity:0];

}

return _dataSource;

}

-(NSMutableArray *)selectData{

if (!_selectData) {

_selectData = [[NSMutableArray alloc] initWithCapacity:0];

}

return _selectData;

}

@end

最后編輯于
?著作權(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)容

  • 概述在iOS開發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 前言 最近忙完項(xiàng)目比較閑,想寫一篇博客來(lái)分享一些自學(xué)iOS的心得體會(huì),希望對(duì)迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,582評(píng)論 9 5
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,728評(píng)論 1 14
  • 昨天問起陽(yáng)洋,教師節(jié)是否與老師有約?未想晚上睡夢(mèng)中又再次與老師相遇~~如果說(shuō),人生的美好,在于遇見,我想...
    艷er_f294閱讀 288評(píng)論 0 1
  • 十二、陪我出去玩 見夏顧不得香蕉皮,嚇得趕緊回頭。 不是被這聲怪叫嚇的。是李燃。李燃現(xiàn)在可不能出現(xiàn),雖然清者自清,...
    雨寧李閱讀 1,674評(píng)論 0 3

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