使用UITableViewStyleGrouped
样式时候如果要自定义组的头试图,那么要
同时
通过代理方法返回组的头试图
、尾试图
,以及组头试图的高度
和尾试图的高度
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *header = [UILabel new];
header.text = @"系统消息";
header.font = [UIFont systemFontOfSize:13.0f];
header.textColor = HexColor(0x666666);
header.backgroundColor = [UIColor clearColor];
return header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
}
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 37.f;
}
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
// 懒加载
- (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - KSafeTabbarHeight) style:UITableViewStyleGrouped];
[_tableView registerClass:[VSUMessageTableViewCell class] forCellReuseIdentifier:kMessageTableViewCellIdenrifier];
_tableView.backgroundColor = kRandomColor;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 80;
_tableView.tableFooterView = [UIView new];
_tableView.showsVerticalScrollIndicator = NO;
}
return _tableView;
}
效果图

自定义cell 的点击态颜色
在cell 中
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UIView *selectView = [[UIView alloc]initWithFrame:self.bounds];
selectView.backgroundColor = [UIColor cyanColor];
self.selectedBackgroundView = selectView;
}
return self;
}
在控制器中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 取消cell 的选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
网友评论