【问题标题】:First grouped table view section header not showing第一个分组表视图部分标题未显示
【发布时间】:2016-05-19 06:11:33
【问题描述】:

我有一个分组表视图,我想自定义部分标题。但我无法显示表格视图的第一部分标题。

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

我看到了类似的问题以及通过实施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
    }

我正在情节提要中实现我的自定义部分标题,然后通过插座引用它:

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

【问题讨论】:

    标签: ios uitableview swift2 sections xcode7.1


    【解决方案1】:

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

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

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

    【讨论】:

    • 但是为什么我不能使用我的故事板中的视图,它有@IBOutlet var sectionHeaderView: ContactTableViewSectionHeaderView!参考?它有一个类“ContactTableViewSectionHeaderView”,它是 UIView 的子类
    • 因为它只是 UIView 的单个实例,您只能使用一次。或者创建新实例。
    • 好吧 viewForHeaderInSection 就像重用一个表格视图单元格一样工作?但是我应该能够从情节提要中使用我自己定制的 UIView 子类吗?我想在情节提要中设计它,然后将其实现为节标题。
    • ContactTableViewSectionHeaderView 不支持来自 XIB 的视图 & 是的,它与重用表格视图单元格相同。
    • 如果我的回答解决了您的问题,您可以接受并投票。
    【解决方案2】:

    让 titleFirstLabel: UILabel = {
    让标签 = UILabel()
    label.text = "文本"
    label.frame = CGRect(x: 15, y: 10, 宽度: 100, 高度: 20)
    返回标签
    }()

    和 viewDidLoad() 添加这段代码

    tableView.addSubview(titleFirstLabel)

    这对我有用。祝你好运:)

    【讨论】:

    • 我不熟悉该技术,但我认为您应该让用户知道应该在哪里进行这些修改。
    猜你喜欢
    • 2019-10-03
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多