简体   繁体   中英

First grouped table view section header not showing

I have a grouped table view which I want to have customized section headers. But I'm having trouble showing the first section header for the table view.

I have two sections and the section header for the first section is not showing but the second one is:

在此处输入图片说明

I have seen similar problems and those problems where solved by implementing the heightForHeaderInSection but I have implemented that.

I'm setting the sections like this:

    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
    }

I'm implementing my custom section header in the storyboard and then referencing it by an outlet:

在此处输入图片说明

Edited: I want to be able to design my custom section header in the storyboard and not programmatically.

Problem is inside viewForHeaderInSection method. You need to create new instance of UIView & need to return it.

OR

You can create Class for UITableViewHeaderHeaderView and Reuse it.

Feel free to ask if you have any doubts regarding this.

let titleFirstLabel: UILabel = {
let label = UILabel()
label.text = "Text"
label.frame = CGRect(x: 15, y: 10, width: 100, height: 20)
return label
}()

and viewDidLoad() add this code

tableView.addSubview(titleFirstLabel)

That work for me. Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM