0

converting my code to swift ONLY so no use of storyboards. After rewriting the code (no storyboard usage) the header information is NOT shown !!

If I change the viewForHeaderInSection return value from:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
    cell.fillHeader(header: listTitles[section])

    return cell.contentView
}

to:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
    cell.fillHeader(header: listTitles[section])

    return cell  // ==> removed .contectView
}

the header content is shown BUT the section header moves left performing a swipe gesture that I use to initiate a row delete. Any suggestions? Find the relevant code below.

class ListsVC: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(ListsVCHeader.self, forCellReuseIdentifier: "headerId")
        // register other Cells
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return listTitles.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
    -> String? {
        return listTitles[section]
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
        cell.fillHeader(header: listTitles[section])
        return cell.contentView
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return HeaderSectionHeight
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        switch editingStyle {
        case .delete:
            // mycode
        default:
            break
        }
    }
}

the Section Header cell

class ListsVCHeader: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)
        // add subviews and constraints
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func fillHeader (header: String) {
        name.text = header
    }
}
Koen
  • 71
  • 7
  • Why do you have both titleForHeaderInSection & viewForHeaderInSection methods implemented? – Devster101 Mar 23 '17 at 16:46
  • Well spotted. If I only use: titleForHeaderInSection it works. But I have another issue. The system is using the default font size of the label and I need the label not vertical centered but 30pt from the top. To implement this I used viewForHeaderInSection. Any suggestion to change the header section font and have custom constraints for the header section text using titleForHeaderInSection ? – Koen Mar 23 '17 at 18:29

1 Answers1

1

Change your register in viewDidLoad to:

tableView.register(ListsVCHeader.self, forHeaderFooterViewReuseIdentifier: "headerId") 

Your class ListsVCHeader should be changed to:

class ListsVCHeader: UITableViewHeaderFooterView

and last change the following code:

let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader

to

let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId) as! ListsVCHeader
Hapeki
  • 2,153
  • 1
  • 20
  • 36
  • No problem! Could you please up vote and mark the answer as correct so others with the same problem know too? :) – Hapeki Mar 23 '17 at 19:09