2

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:

enter image description here

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:

enter image description here

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

2 Answers2

0

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.

Vishal Sharma
  • 1,733
  • 2
  • 12
  • 32
  • But why can't I use my view from the storyboard that has the @IBOutlet var sectionHeaderView: ContactTableViewSectionHeaderView! reference? It has a class "ContactTableViewSectionHeaderView" which is a subclass of UIView –  Feb 09 '16 at 10:32
  • Cause it's only single Instance of UIView & You can use it only once. Or create new instance. – Vishal Sharma Feb 09 '16 at 10:35
  • Ok so viewForHeaderInSection works like reusing a table view cell? But I should be able to use my own customized subclass of UIView from the storyboard? I want to design it in the storyboard and then implement it as section header. –  Feb 09 '16 at 11:41
  • View from XIB is not supported with ContactTableViewSectionHeaderView & Yes it works same as reusing a table view cell. – Vishal Sharma Feb 09 '16 at 13:29
  • You can accept & Up-vote if my answer salved your question. – Vishal Sharma Feb 09 '16 at 13:31
  • http://samwize.com/2015/11/06/guide-to-customizing-uitableview-section-header-footer/ Here's a great guide for this (in swift) – Michael McKenna Jul 22 '16 at 17:36
  • I have used `viewforHeaderinsection` method and created instance of `uiview` and returned it. But I wanted not to stick the `tableview header` for every section, so i changed my `tableview` style to `grouped`. Now everything works fine but the `headerview` for first section is not showing. Can you tell me where the problem is? @SharmaVishal – iPeter Apr 05 '18 at 14:27
-1

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 :)

  • I'm not familiar with that technology, but I think you should let the user know where these modifications should be done. – Tipx May 07 '17 at 06:20