【发布时间】:2023-04-08 15:38:01
【问题描述】:
基本上我想更改节标题的字体和颜色,所以我实现了tableVieW:viewForHeaderInSection。首先我尝试了这段代码:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel* headerLabel = [[[UILabel alloc] init] autorelease];
headerLabel.frame = CGRectMake(10, 0, 300, 40);
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = @"My section header";
return headerLabel;
}
但由于某种原因,frame 属性被忽略(我说的是左侧的 10px 插图)。现在我使用以下内容:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
UILabel* headerLabel = [[UILabel alloc] init];
headerLabel.frame = CGRectMake(10, 0, 300, 40);
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = @"My section header";
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
达到预期的效果。有人可以向我解释为什么第二种方法有效而第一种方法无效吗?
PS。在这两种情况下,我也都实现了tableView:heightForHeaderInSection,返回 40.0
【问题讨论】:
标签: iphone uitableview frame