iOS開(kāi)發(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