簡體   English   中英

第一個分組的表格視圖節標題未顯示

[英]First grouped table view section header not showing

我有一個分組的表格視圖,我想擁有自定義的節標題。 但是我在顯示表格視圖的第一部分標題時遇到了麻煩。

我有兩個部分,第一部分的部分標題未顯示,但第二部分是:

在此處輸入圖片說明

我已經看到了類似的問題,並且通過實現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
    }

我正在情節提要中實現我的自定義節標題,然后通過插座引用它:

在此處輸入圖片說明

編輯:我希望能夠在情節提要中而不是通過編程方式設計自定義節標題。

問題在viewForHeaderInSection方法內部。 您需要創建UIView的新實例並需要將其返回。

要么

您可以為UITableViewHeaderHeaderView創建類並重用它。

隨時詢問您對此是否有任何疑問。

讓titleFirstLabel:UILabel = {
讓標簽= UILabel()
label.text =“文字”
label.frame = CGRect(x:15,y:10,寬度:100,高度:20)
返回標簽
}()

和viewDidLoad()添加此代碼

tableView.addSubview(titleFirstLabel)

那對我有用。 祝好運 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM