iOS - 在 2 个彼此相邻的表格视图中显示标题视图

问题描述 投票:0回答:1

我有一个视图控制器,它并排显示两个

tableView

我还有一个从

ButtonHeaderFooterView
延伸的类 (
UITableViewHeaderFooterView
)。

我想根据表格视图显示不同的标题标题,但这对我不起作用。

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

这是我所看到的屏幕截图。

enter image description here

你知道我做错了什么吗?

ios swift uitableview tableview swift5
1个回答
0
投票

我认为问题可能与

cachedHeader
ivar 有关。我认为它需要与以下内容一起删除:

  • func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
    ,以及
  • func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
    方法。

您可以在使标题视图出队后立即设置字符串。

可能类似的东西会是更好的选择:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet private var leftTableView: UITableView!
    @IBOutlet private var rightTableView: UITableView!
    
    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, viewForHeaderInSection section: Int) -> UIView? {
        guard let headerView = ButtonHeaderFooterView.dequeue(forTable: tableView) else {
            fatalError("Failed to dequeue header/footer view buttonHeaderFooterView")
        }

        if tableView == leftTableView {
            headerView.textLabel?.text = "Test 1"
        } else {
            headerView.textLabel?.text = "Test 2"
        }

        return headerView
    }
        
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 36
    }
}

不需要缓存标题视图,因为表视图会为您做这件事。这就是为什么您需要首先将标题视图出队。

© www.soinside.com 2019 - 2024. All rights reserved.