Why my TableView doesn't show section headings?

Hello:


I am trying to work with TableView. I need to divide the UI into static headings with text fields under each heading for the user to enter information as instructed by the heading. However, since I am new to using the tableView, I am using a tutorial to learn. The problem is when I run the code the the headings do not display. Below is the code and the results:



//

// ViewController.swift

// CoreDataTableView

//

// Created on 5/26/19.

import UIKit


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let sections = ["Gains","Losses"]

let items = [

["Dogs","Cats","Tigers","Lion"],

["Bats","Eagles","Doves","Hawks"]

]

@IBOutlet weak var decisionsTableView: UITableView!

override func viewDidLoad() {

super.viewDidLoad()

decisionsTableView.delegate = self

decisionsTableView.dataSource = self

}

private func tableView(_ tableView: UITableView, titleForHeadersInSection section: Int) -> String?

{

return "Section \(section + 1)"

}

func numberOfSections(in tableView: UITableView) -> Int {

return self.sections.count

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return items[section].count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

cell.textLabel?.text = items[indexPath.section][indexPath.row]

return cell

}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

view.tintColor = UIColor.red

let header = view as! UITableViewHeaderFooterView

header.textLabel?.textColor = UIColor.white

}

}


I need some help to move forward.


Thanks

Replies

To display headers for sections, you need to implement `tableView(_:titleForHeaderInSection:)`.

Hello 00Per:


Appreciate your feedback but how is what you recommended different fom what I have in the code?

Please search with `titleForHeaderInSection` on this page.

an extra s can make a lot of difference … 😉