2011-06-14 83 views
6

我正在嘗試爲UITableView創建自定義節標題。我已經找到了幾個參考文件來說明如何在代碼中完成這個操作(#1349571)。如何用XIB自定義tableView Section Header?

我想確定是否有可能在Interface Builder中創建一個UIView,並使用它來自定義類似於如何爲UITableViewCell執行此操作的頭文件?

回答

7

YES可以使用使用XIB創建的標題視圖。創建一個XIB和類來管理XIB(UIView類)。使用

YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject]; 

//With this method you can load any xib for header view 

tableView.tableHeaderView = v; 
[v release]; 

編輯

回到這一觀點在這樣

YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject]; 
//Do some stuff here like setting text on labels etc. 
return [v autorelease]; 
0

的viewForHeaderInSection其添加爲表上方的標籤和刪除擱淺表頭我這樣做,一旦我知道它聲音很奇怪,但工作

3

由於我們有原型單元格,您還可以添加一個原型單元格到您的表。 在Interface Builder中填寫原型單元的標識符。 (例如HeaderCell) 然後,您可以像使用cellForRowAtIndexPath中的單元格一樣在viewForHeaderForSection中使用它。

例子:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [tableView dequeueReusableCellWithIdentifier:HeaderCellID]; 
    UILabel *label = (UILabel *)[headerView viewWithTag:100]; 
    label.text = [self tableView:self.tableView titleForHeaderInSection:section]; 

    return headerView; 
} 
+0

不知道爲什麼我從來沒有想到這一點。對於任何複雜的頭文件來說工作得很好,而且更容易,特別是在使用自動佈局時,會取出很多常量代碼。謝謝! – WCByrne 2014-10-07 00:33:45

相關問題