iOS自定义组头视图

实现目标:组头文字

方法一:实现代理方法 -> table: titleForHeaderInSection: ,直接返回表头字符串即可。

- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;        

方法二:创建自定义tableView类

.h文件

@interface WLCommentHeaderView : UITableViewHeaderFooterView

@property (nonatomic, strong) UILabel *label;

@property (nonatomic,copy)NSString *text;

.m文件

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithReuseIdentifier:reuseIdentifier];

if (self) {

[self createSubViews];

}

return self;

}

// 创建子视图

- (void)createSubViews {

self.label = [[UILabel alloc] initWithFrame:CGRectZero];

[self.contentView addSubview:_label];

self.label.textColor = [UIColor blackColor];

self.label.textAlignment = NSTextAlignmentCenter;

}

// Layout布局

- (void)layoutSubviews {

[super layoutSubviews];

CGFloat width = CGRectGetWidth(self.contentView.bounds);

CGFloat height = CGRectGetHeight(self.contentView.bounds);

self.label.frame = CGRectMake(width / 4, 10, width / 2, height - 10);

}

-(void)setText:(NSString *)text

{

_text=[text copy];

self.label.text=_text;

}

调用:

注册自定义组头视图

[tableview registerClass:[WLCommentHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

WLCommentHeaderView *header=[[WLCommentHeaderView alloc]initWithReuseIdentifier:@"header"];

if (0 == section) {

header.text=@"生活服务";

}

return header;

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容