iOS UITableView组头组尾小结
项目中我们会经常用到UITableView组头组尾,下边简述下三种方式(组头为例,组尾同理)
一、继承UITableViewHeaderFooterView(正规写法,有复用,适合组头频繁出现的情况)
1、自定义SectionHeaderView
@interface SectionHeaderView : UITableViewHeaderFooterView @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UILabel *titleLab; @end
@implementation SectionHeaderView #pragma mark - System - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithReuseIdentifier:reuseIdentifier]; if (self) { [self initData]; [self initUI]; } return self; } #pragma mark - Init Data - (void)initData { } #pragma mark - Init UI - (void)initUI { UIView *bgView = [[UIView alloc]init]; bgView.backgroundColor = XZWL_COLOR_FFFFFF; [self.contentView addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 15, 0, 15)); }]; self.bgView = bgView; UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"career_addcustomer_sectionHead"]]; [bgView addSubview:icon]; [icon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.top.equalTo(bgView.mas_top).offset(17); make.width.height.mas_equalTo(20); }]; self.titleLab = [[UILabel alloc]init]; self.titleLab.textColor = XZWL_COLOR_333333; self.titleLab.font = FF_PFM_ICOME(16); self.titleLab.numberOfLines = 2; self.titleLab.lineBreakMode = NSLineBreakByCharWrapping; [bgView addSubview:self.titleLab]; [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(icon.mas_right).offset(15); make.right.equalTo(bgView.mas_right).offset(-15); make.height.mas_greaterThanOrEqualTo(20); make.top.equalTo(icon); }]; UIView *line = [[UIView alloc]init]; line.backgroundColor = XZWL_COLOR_EBEBEB; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(bgView); make.height.mas_equalTo(0.5); }]; }
2、使用
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { SectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Test"]; if (headerView == nil) { headerView = [[SectionHeaderView alloc] initWithReuseIdentifier:@"Test"]; } headerView.titleLab.text = @"这是组头"; return headerView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; }
二、自定义UIView(野路子,适合简单的组头,或者只出现一次,不重复使用的)
1、创建
@interface SectionHeader : UIView @property (nonatomic, strong) UILabel *titleLable; @end
@implementation SectionHeader #pragma mark - System - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initData]; [self initUI]; } return self; } #pragma mark - Init Data - (void)initData { } #pragma mark - Init UI - (void)initUI { UILabel *msgLabel = [[UILabel alloc] init]; msgLabel.frame = CGRectMake(15, 0, 200, 16); msgLabel.font = FF_PFM_ICOME(17); msgLabel.textColor = XZWL_COLOR_333333; self.backgroundColor = [UIColor clearColor]; [self addSubview:msgLabel]; self.titleLable = msgLabel; } @end
2、使用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 36; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//不要试图使用懒加载创建,然后动态改变title,这样会出问题,因为每一个组头都需要一个对象,正规写法是有复用机制的
SectionHeader *tableSectionHederView = [[SectionHeader alloc] init];
NSString *title = @"Test1"; if (section == 2) { title = @"Test2"; } tableSectionHederView.titleLable.text = title; return tableSectionHederView; }
三、直接返回UIView(野路子,适合极简单的,与二相比,未封装,冗余)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44.0; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] init]; headerView.frame = CGRectMake(0, 0, LL_ScreenWidth, 44.0); headerView.backgroundColor = THEME_COLOR_WHITE; headerView.clipsToBounds = YES; UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(15, 13, LL_ScreenWidth - 150, 18); titleLabel.font = [UIFont systemFontOfSize:17]; titleLabel.numberOfLines = 1; titleLabel.textColor = XZWL_COLOR_999999; titleLabel.textAlignment = NSTextAlignmentLeft; [headerView addSubview:titleLabel]; UIView *lineView = [[UIView alloc] init]; lineView.frame = CGRectMake(0, 43.5, LL_ScreenWidth, 0.5); lineView.backgroundColor = XZWL_COLOR_EBEBEB; [headerView addSubview:lineView]; return headerView; }