iOS自定義組頭視圖

實現(xiàn)目標(biāo):組頭文字

方法一:實現(xiàn)代理方法 -> table: titleForHeaderInSection: ,直接返回表頭字符串即可。

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

方法二:創(chuàng)建自定義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;

}

// 創(chuàng)建子視圖

- (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;

}

調(diào)用:

注冊自定義組頭視圖

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

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

{

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

if (0 == section) {

header.text=@"生活服務(wù)";

}

return header;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容