2
votes

I have a table view that is subclassed and extended, then being set up in the View controller. The problem that I'm having is the delegate method ViewForHeaderInSection isn't being called, while the normal data source methods are being called.

(this is the TableView setup method, is called in ViewDidLoad. The table view is connected to the View Controller with IBOutlet)

func setup() {
    self.dataSource = self as UITableViewDataSource
    self.delegate = self
    let nib = UINib(nibName: "MyTableViewCell", bundle: nil)
    self.register(nib, forCellReuseIdentifier: "MyCell")

}

These are the extensions

extension MyTableView: UITableViewDataSource,UITableViewDelegate {


    func numberOfSections(in tableView: UITableView) -> Int {
        //print(Genres.total.rawValue)
        return Genres.total.rawValue
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let tableSection = Genres(rawValue: section), let articleData = articleDictionary[tableSection]  {
          // print(articleData.count)
            return articleData.count
        }
        print(0)
        return 0
    }


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

         let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyTableViewCell

        if let tableSection = Genres(rawValue: indexPath.section), let article = articleDictionary[tableSection]?[indexPath.row]{
            cell.cellTitle.text = article.articleTitle
            cell.cellImageView.image = article.articleImage
            cell.cellEmojiReaction.text = article.articleEmojis
        }


        return cell
    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 40.0))
        view.backgroundColor = .brown
        let label = UILabel(frame: CGRect(x: 16, y: 0, width: tableView.bounds.width, height: 40))
        label.textColor = .blue

        let tableSection = Genres(rawValue: section)
        switch tableSection {
        case .breakingNews?:
            label.text = "Breaking News"
        case .scienceAndTech?:
            label.text = "Science and Tech"
        case .entertainment?:
            label.text = "Entertainment"
        case .sports?:
            label.text = "Sports"
        default:
            label.text = "Invalid"
        }
        print("Function Run")
        view.addSubview(label)
        print(label.text ?? "Nothing Here")
        return view
    }

}

Here is the View controller Code:

class ViewController: UIViewController {


    @IBOutlet weak var myTableView: MyTableView!

    override func viewDidLoad() {
        super.viewDidLoad()
       myTableView.setup()
    }

}

Is there any specific reason why the delegate method isn't being called? Thank you in advance for your time.

3
Please check my answer.Saurabh Jain

3 Answers

1
votes

For this you have to also implement one more method heightForHeaderInSection along with viewForHeaderInSection method.

0
votes

If you are using viewForHeaderInSection delegate method then it is compulsory to use heightForHeaderInSection method other wise section header mehtod is not called

Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView(_:viewForHeaderInSection:) returned a nil view. In iOS 5.0 and later, you must return the actual height for each section header in this method.

Official Link for more description https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614855-tableview

-1
votes

Add in viewDidLoad:

tableView.estimatedSectionHeaderHeight = 80