帧总是相同的尺寸

问题描述:

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

if(section != 0) { 

    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease]; 
    view.backgroundColor = [UIColor redColor]; 

    return view; 

} else { 
    return tableView.tableHeaderView; 
} 

}帧总是相同的尺寸

这是我实现viewForHeaderInSection的但无论帧我让它总是显示我相同的红色框。你看到我的代码有问题吗?

图片:

enter image description here

UPDATE:

MHM现在我的红块较高,但我第一次的tableHeader现在以某种方式隐藏。第一个是用titleForHeaderInSection实现的。我想我只需要实现的tableHeader高度的高度,但是,这并不工作

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
if(section == 1) 
    return 30; 
else 
    return tableView.tableHeaderView.frame.size.height; 
} 

你需要实现这个委托方法

- (CGFloat)tableView:(UITableView *)tableView 
heightForHeaderInSection:(NSInteger)section; 

在你的情况,你可以简单地return 30;


此外,你泄露view

您的[view release]发生在return之后。但只要return发生,方法执行就会中止,并且您的release永远不会被调用。

所以,你想这不是

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease]; 

,摆脱明确release楼下的。

+0

thx,你知道他们为什么不采取无耻的身高吗?不明白为什么我必须设置一个额外的代表方法的高度... – gabac 2010-03-15 20:15:15

+0

我不知道为什么苹果这样做。这有点傻。但我认为他们希望标题框架*始终*跨越表格视图的整个宽度。所以剩下的唯一大小的变量就是高度。 – 2010-03-15 20:18:09

+0

thx关于autorelease的提示。但是现在我有另外一个问题了,也许你可以再帮我一次吗? – gabac 2010-03-15 20:23:22