ios - 第一个分组 TableView 部分标题未显示

标签 ios uitableview swift2 sections xcode7.1

我有一个分组 TableView ,我想要自定义部分标题。但是我无法显示表格 View 的第一节标题。

我有两个部分,第一部分的部分标题未显示,但第二部分是:

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
    }

我在 Storyboard 中实现我的自定义部分标题,然后通过导出引用它:

enter image description here

编辑: 我希望能够在 Storyboard 中设计我的自定义部分标题,而不是以编程方式。

最佳答案

问题出在 viewForHeaderInSection 方法中。 您需要创建 UIView 的新实例并需要返回它。

您可以为 UITableViewHeaderHeaderView 创建类并重用它。

如果您对此有任何疑问,请随时询问。

关于ios - 第一个分组 TableView 部分标题未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35288997/

相关文章:

ios - 如何使用 alamofire 图像从 json 数组检索图像?

ios - 应用程序崩溃后检查 XCODE 中的变量

ios - 适用于 iOS 7 和 iOS 8 的警报

java - 每个单元格等于一个对象而不是行的表格

ios - UIView 与 UITableView 和 UIGestureRecognizer 突然停止手势识别

Ios DBAccess 查询调试

ios - UITableView 底部奇怪的空白空间

arrays - 无法分配 String 类型的值 :NSObject to type String:NSObject

ios - 在 Swift 2 中重新加载数据时,如何防止表格格式发生变化?

swift - 在协议(protocol)扩展中将方法标记为 final