Basically I want to change the font and the color of my section header, so I implement tableVieW:viewForHeaderInSection
. First I tried this code:
基本上,我想要改变section header的字体和颜色,所以我实现了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;
}
but for some reason the frame property is ignored (I'm talking about the 10px inset on the left). Now I use the following:
但是由于某种原因,frame属性被忽略(我说的是左边的10px inset)。现在我使用以下方法:
-(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;
}
with the desired results. Can someone explain to me why the second approach works and the first doesn't?
与预期的结果。有人能解释一下为什么第二种方法有效,而第一种方法无效吗?
PS. In both cases I implement tableView:heightForHeaderInSection
as well, returning 40.0
在这两种情况下,我都实现了tableView:heightForHeaderInSection,返回40.0。
2 个解决方案
#1
26
That's because the UITableView automatically sets the frame of the header view you provide to
这是因为UITableView会自动设置你提供给头视图的框架
(0, y, table view width, header view height)
(0, y,表视图宽度,页眉视图高度)
y
is the computed position of the view and header view height
is the value returned by tableView:heightForHeaderInSection:
y是视图的计算位置,头视图高度是tableView返回的值:heightForHeaderInSection:
#2
1
Maybe it's better do add a subview:
也许最好添加一个子视图:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let label = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width, height: 20))
label.text = "\(sections[section].year)"
view.addSubview(label)
return view
}
#1
26
That's because the UITableView automatically sets the frame of the header view you provide to
这是因为UITableView会自动设置你提供给头视图的框架
(0, y, table view width, header view height)
(0, y,表视图宽度,页眉视图高度)
y
is the computed position of the view and header view height
is the value returned by tableView:heightForHeaderInSection:
y是视图的计算位置,头视图高度是tableView返回的值:heightForHeaderInSection:
#2
1
Maybe it's better do add a subview:
也许最好添加一个子视图:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let label = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width, height: 20))
label.text = "\(sections[section].year)"
view.addSubview(label)
return view
}