I read every questions concerning this issue and none of the answers solved my issue.
I'm trying to implement a custom header view to my tableView
. However the method viewForHeaderInSection
is not called for no reason.
I added the tableView
datasource and delegate to the viewController
.
I removed the lines of code the add data to my arrays because it would be too long. Consider my arrays are filled. with some My code:
class TransactionsViewController: UIViewController {
lazy var tableView = UITableView()
var sections = [TransactionsMonthSection]()
var transactions = [Transaction]()
// ...
override func viewDidLoad() {
super.viewDidLoad()
// adding some data here ...
commonInit()
tableView.reloadData()
}
private func commonInit() {
view.backgroundColor = .clear
tableView.backgroundColor = .systemBackground
tableView.rowHeight = 74
tableView.sectionHeaderHeight = 50
tableView.register(TransactionCell.self, forCellReuseIdentifier: cellId)
tableView.register(TransactionsHeaderView.self, forHeaderFooterViewReuseIdentifier: TransactionsHeaderView.reuseId)
tableView.delegate = self
tableView.dataSource = self
tableView.contentInsetAdjustmentBehavior = .never
tableView.separatorInset = UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 16)
tableView.tableFooterView = UIView()
view.addSubview(tableView)
}
}
extension TransactionsViewController: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].transactions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? TransactionCell else {
fatalError("The dequeued cell is not an instance of TransactionCell.")
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: TransactionsHeaderView.reuseId) as! TransactionsHeaderView
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
}
The method heightForHeaderInSection
is called but not viewForHeaderInSection
.
Also, if I remove this line: tableView.sectionHeaderHeight = 50
, the method heightForHeaderInSection
is not called too.
Is that a bug from Xcode? I never seen that before.
The reason why the tableView
's delegate method viewForHeaderInSection
wasn't called is because I was adding the tableView'
s delegate in another viewController
.
In fact this TransactionsViewController
is implemented as a child in another viewController
and I need its delegate there. The issue occurred because I was doing this in the parent VC:
transactionsVC.tableView.delegate = self
I'm don't really know why it prevents viewForHeaderInSection
to be called though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.