I have a view controller that shows two tableView
s side by side.
I have also a class (ButtonHeaderFooterView
) that extends from UITableViewHeaderFooterView
.
I want to show different titles for the header depending on the table view, but it’s not working for me.
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet private var leftTableView: UITableView!
@IBOutlet private var rightTableView: UITableView!
private var cachedHeader: ButtonHeaderFooterView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
ButtonHeaderFooterView.register(forTable: leftTableView)
ButtonHeaderFooterView.register(forTable: rightTableView)
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if tableView == leftTableView {
return ""
}
return ""
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let cachedHeader = cachedHeader {
return cachedHeader
}
guard let headerView = ButtonHeaderFooterView.dequeue(forTable: tableView) else {
fatalError("Failed to dequeue header/footer view buttonHeaderFooterView")
}
cachedHeader = headerView
return headerView
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? ButtonHeaderFooterView else {
return
}
if tableView == leftTableView {
header.textLabel?.text = "Test 1"
} else {
header.textLabel?.text = "Test 2"
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 36
}
}
ButtonHeaderFooterView.swift
import Foundation
import UIKit
protocol ButtonHeaderFooterDelegate: AnyObject {
func buttonTapped()
}
class ButtonHeaderFooterView: UITableViewHeaderFooterView {
static let identifier = "ButtonHeaderFooterView"
@IBOutlet private var titleLabel: UILabel!
static func dequeue(forTable tableView: UITableView) -> ButtonHeaderFooterView? {
tableView.dequeueReusableHeaderFooterView(withIdentifier: ButtonHeaderFooterView.identifier) as? ButtonHeaderFooterView
}
static func register(forTable tableView: UITableView) {
tableView.register(UINib(nibName: "ButtonHeaderFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: ButtonHeaderFooterView.identifier)
}
}
Here’s a screenshot of what I see.
Do you know what I’m doing wrong?