基本上,我想更改节标题的字体和颜色,因此我实现了 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
最佳答案
这是因为 UITableView 会自动设置您提供的标题 View 的框架
(0,y,表格 View 宽度,标题 View 高度)
y
是 View 的计算位置,
header view height
是 tableView:heightForHeaderInSection:
返回的值
关于iphone - 为什么tableViewW :viewForHeaderInSection ignore the frame property of my UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4970255/