前言
在平時的開發(fā)tableview可以說是用到的最多的控件了,而且tableview的單選和多選也并不少見种冬,今天就來討論下tableview的單選和多選镣丑。
單選
單選的邏輯其實就是記錄選中的indexpath,然后當(dāng)用戶選擇cell的時候把上次選中的cell取消碌廓,選中本次選擇的cell
#import "SingleSelectViewController.h"
@interface SingleSelectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@end
@implementation SingleSelectViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"single select";
[self setupViews];
}
- (void)setupViews{
for (NSInteger i = 0; i < 10; i ++) {
NSString *str = [NSString stringWithFormat:@">>>>>%lu",i];
[self.dataArray addObject:str];
}
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableFooterView = [UIView new];
self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
[self.view addSubview:self.tableView];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.selectedIndexPath && self.selectedIndexPath == indexPath) {
cell.textLabel.textColor = [UIColor blueColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.textLabel.textColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
/*NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];*/ /*如果是點擊cell上子控件可用此方法找到對應(yīng)path*/
if (self.selectedIndexPath) {
cell = [self.tableView cellForRowAtIndexPath:self.selectedIndexPath];
cell.textLabel.textColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor blueColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndexPath = indexPath;
[self.tableView reloadData];
}
- (NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
多選
多選的邏輯也不復(fù)雜传轰,主要是再編輯狀態(tài)下記錄用戶選擇了哪些cell,在editingStyleForRowAtIndexPath
里返回UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert
就可以設(shè)置多選谷婆,多選的時候只要操作相應(yīng)的數(shù)據(jù)就可以了
#import "MoreSelectViewController.h"
@interface MoreSelectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *selectArray;
@end
@implementation MoreSelectViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"more select";
[self setupViews];
}
- (void)setupViews{
for (NSInteger i = 0; i < 10; i ++) {
NSString *str = [NSString stringWithFormat:@">>>>>%lu",i];
[self.dataArray addObject:str];
}
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"編輯" style:UIBarButtonItemStyleDone target:self action:@selector(editAction:)];
self.navigationItem.rightBarButtonItem = item;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableFooterView = [UIView new];
self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
[self.view addSubview:self.tableView];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = self.dataArray[indexPath.row];
[self.selectArray addObject:str];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = self.dataArray[indexPath.row];
[self.selectArray removeObject:str];
}
- (void)editAction:(UIBarButtonItem *)item{
if ([item.title isEqualToString:@"編輯"]) {
if (self.dataArray.count == 0) {
return;
}
item.title = @"取消";
[self.tableView setEditing:YES animated:YES];
}else{
item.title = @"編輯";
[self.tableView setEditing:NO animated:YES];
}
NSLog(@">>>>>>>>>>selectArrayCount : %lu",self.selectArray.count);
}
- (NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (NSMutableArray *)selectArray{
if (!_selectArray) {
_selectArray = [NSMutableArray array];
}
return _selectArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Demo在這里下載
總結(jié)
以上就是對TableView單選和多選的一些總結(jié)慨蛙,如果有寫的不好的地方,請多多指正纪挎。