前言:
很久以前学习自定义 cell 的时候,了解了 cell 选中样式, cell 里面还有一个 selectedBackgroundView 这样的属性,这个属性你得 alloc init 一个新的 view 放上去才能好用,反正摸索一遍之后感觉不是很愉快,在搬砖的时候,竟然花费了我一些时间。于是寻求最简洁的设置 cell 选中样式的方法,和设置 cell 默认选中的方法。
代码地址:https://github.com/gityuency/ObjectiveCTools
类名【SelectionTableViewController】
效果图:
选中.gif
第一步: 创建控制器,添加 UItableView。
1、设置默认选中 cell 关键代码是
[self.tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop];
2、注意:如果自己寻找 path 然后根据 path 取出 cell, 使用这样的方法:
cell.selected = YES;
[cell setSelected:YES animated:YES];
会出现cell 无法自动取消选中的情况。
3、如果要让 cell 设置默认选中之后触发 代理 didSelectRowAtIndexPath,那么需要你手动调用这个代理方法。
控制器代码如下:
#import "SelectionTableViewController.h"
#import "SelectionCell.h"
@interface SelectionTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation SelectionTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[self.tableView registerClass:[SelectionCell class] forCellReuseIdentifier:[SelectionCell forCellWithReuseIdentifier]];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:0];
// 如果要让默认 cell 选中同时触发选中事件,需要手动调用 didSelectRowAtIndexPath
if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:path];
}
// 设置默认选中的 cell
[self.tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:[SelectionCell forCellWithReuseIdentifier] forIndexPath:indexPath];
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:@"分组头%ld",section];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"选中事件");
}
@end
第二步: 自定义 cell
关键步骤:
1、设置选中样式为StyleNone:
self.selectionStyle = UITableViewCellSelectionStyleNone;
2、自定义选中样式,重写:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
cell代码如下:
#import "SelectionCell.h"
@implementation SelectionCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
self.textLabel.text = @"选中";
self.textLabel.textColor = [UIColor redColor];
self.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
self.textLabel.text = @"somebody help!";
self.textLabel.textColor = [UIColor blackColor];
self.accessoryType = UITableViewCellAccessoryNone;
}
}
+ (NSString *)forCellWithReuseIdentifier {
return NSStringFromClass(SelectionCell.class);
}
@end