繁体   English   中英

未调用 tableview 方法 viewForHeaderInSection - Swift 5 / iOS14

[英]The tableview method viewForHeaderInSection is not called - Swift 5 / iOS14

我阅读了有关此问题的所有问题,但没有一个答案解决了我的问题。

我正在尝试对我的tableView实现自定义 header 视图。 但是方法viewForHeaderInSection不会无缘无故地被调用。

我添加了tableView数据源并委托给viewController

我删除了将数据添加到我的 arrays 的代码行,因为它太长了。 考虑我的 arrays 已满。 有一些我的代码:

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
          }
    }

方法heightForHeaderInSection被调用但不是viewForHeaderInSection

此外,如果我删除这一行: tableView.sectionHeaderHeight = 50 ,也不会调用方法heightForHeaderInSection

这是来自 Xcode 的错误吗? 我以前从未见过。

没有调用tableView的委托方法viewForHeaderInSection的原因是因为我在另一个viewController中添加了tableView'的委托。

事实上,这个TransactionsViewController是作为另一个viewController中的子实现的,我需要它的委托。 出现问题是因为我在父 VC 中执行此操作:

transactionsVC.tableView.delegate = self

我真的不知道为什么它会阻止viewForHeaderInSection被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM