2010-03-15 71 views
17
- (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; 
} 

回答

40

你需要實現這個委託方法

- (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