2016-11-07 139 views
1

我需要爲我的tableview製作一個自定義頁眉。爲此,我創建了一類UITableViewHeaderFooterView類型,但我無法選擇故事板上的tableview標題來設置類。如果它是出現靜態表頭,但對動態表不可見。我怎樣才能做到這一點?使用Swift自定義表格視圖的頁眉和頁腳

注:我使用的Xcode 7.3.1

我試圖做這樣的事情,但情節提要: https://github.com/jeantimex/ios-swift-collapsible-table-section

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! NovaListaTableViewHeader 


    header.titleLabel.text = sections[section].name 
    header.arrowLabel.text = ">" 
    header.setCollapsed(sections[section].collapsed) 

    header.section = section 
    header.delegate = self 

    return header 
} 
+0

分享你的代碼並分享你在Stackoverflow上閱讀的類似問題。否則你的問題很快就會被標記出來。 :| – Honey

回答

3

有幾種方法可以做到這一點。大多數是通過編程實現的,有些通過故事板和程序實現。

所以它完全取決於你如何實現它。

我可以與您分享一個最簡單的方法來自定義頁眉和頁腳部分。

,如果你有故事板的嘗試很好的控制,以創建一個

UITableViewCell 

現在裝飾它,只要你想,並在標識名稱現在SectionHeader使用它作爲複用小區

在使用該委託的方法後,我分享一個客觀的C代表,

 override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 


var headerView: SectionHeaderTableViewCell? = tableView.dequeueReusableCellWithIdentifier("SectionHeader") 
    if (headerView == nil) { 
    headerView = SectionHeaderTableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"SectionHeader") 
    } 
    headerView!.textLabel!.text = "Hello World" 


      return headerView; 

     } 

現在做同樣的事情爲頁腳。

+0

這對Xcode 8.3.3和Mac OS 10.12.4 – nyxee

+1

來說並不適用,你忘記提及'SectionHeaderTableViewCel'l應該繼承UITableViewHeaderFooterView' – nyxee

+0

不要忘記覆蓋section中header的方法高度,否則默認情況下返回0.因此,添加如下內容:'override func tableView(_ tableView:UITableView,heightForHeaderInSection section:Int) - > CGFloat { return 60 } –