2016-02-09 105 views
0

我有一個分組表格視圖,我想要自定義節標題。但我無法顯示錶格視圖的第一個部分標題。第一個分組表格視圖區域標題不顯示

我有兩個部分,用於第一部分節頭沒有顯示,但是第二個是:

enter image description here

我看到類似的問題,其中,通過實施heightForHeaderInSection但我解決了這些問題已經實現了。

我設置部分是這樣的:

let kSectionHeaderContact: String = "CONTACT INFORMATION" 
    let kSectionHeaderFeedback: String = "FEEDBACK" 

    enum Sections: Int { 
     case ContactSection = 0 
     case FeedbackSection = 1 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 2 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if section == Sections.ContactSection.rawValue { 
      return contactObjectsDictionary.count 
     } else if section == Sections.FeedbackSection.rawValue { 
      return feedbackObjectsDictionary.count 
     } else { 
      return 0 
     } 
    } 

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     if section == Sections.ContactSection.rawValue { 
      sectionHeaderView.sectionHeaderTitle.text = kSectionHeaderContact 
     } else if section == Sections.FeedbackSection.rawValue { 
      sectionHeaderView.sectionHeaderTitle.text = kSectionHeaderFeedback 
     } 

     return sectionHeaderView 
    } 

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return kContactTableViewSectionHeaderViewHeight 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifierContactTableViewCell, forIndexPath: indexPath) as! ContactTableViewCell 

     // Configure the cell... 
     var titleText: String = "" 
     var detailText: String = "" 

     if indexPath.section == Sections.ContactSection.rawValue { 
      let contactObject = contactObjectsDictionary[indexPath.row] 

      titleText = contactObject[kDictionaryTitleKey]! 
      detailText = contactObject[kDictionaryDetailKey]! 

     } else if indexPath.section == Sections.FeedbackSection.rawValue { 
      let feedbackObject = feedbackObjectsDictionary[indexPath.row] 

      titleText = feedbackObject[kDictionaryTitleKey]! 
      detailText = feedbackObject[kDictionaryDetailKey]! 
     } 

     cell.configureCell(titleText, detail: detailText) 

     return cell 
    } 

我執行在我的故事板自定義欄目標題,然後通過出口引用它:

enter image description here

編輯: 我希望能夠在故事板中設計我的自定義部分標題,而不是以編程方式。

回答

1

問題出在viewForHeaderInSection方法。 您需要創建新實例UIView &需要返回它。

OR

可以爲UITableViewHeaderHeaderView創建類和重用。

隨意問你是否對此有任何疑問。

+0

但爲什麼我不能使用我的視圖從故事板有@IBOutlet var sectionHeaderView:ContactTableViewSectionHeaderView!參考?它有一個類「ContactTableViewSectionHeaderView」,它是UIView的子類 – Oskariagon

+0

因爲它只是UIView的單個實例&你只能使用它一次。或者創建新的實例。 –

+0

好吧,所以viewForHeaderInSection像重用表格視圖單元格一樣工作?但是我應該能夠從故事板中使用我自己定製的UIView子類?我想在故事板中設計它,然後將其實現爲節標題。 – Oskariagon

0

設titleFirstLabel:的UILabel = {
設標記=的UILabel()
label.text = 「文本」
label.frame =的CGRect(X:15,Y:10,寬度:100,高度:20)
返回標籤
}()

和viewDidLoad中()添加以下代碼

tableView.addSubview(titleFirstLabel)

這對我有用。祝你好運:)

+0

我不熟悉該技術,但我認爲你應該讓用戶知道這些修改應該在哪裏完成。 – Tipx

相關問題