基本上我想改变我的部分标题的字体和颜色,所以我实现了tableVieW:viewForHeaderInSection
。首先,我试过这段代码:为什么tableVieW:viewForHeaderInSection忽略我的UILabel的框架属性?
-(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;
}
但由于某些原因框架属性被忽略(我在谈论左边的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
然后我真的需要我的代码中的“headerView”来获得这个插图,对吧? – phi 2011-02-11 15:01:39