如何固定 tableHeaderView?

How I can pin the tableHeaderView?

请告诉我,如何通过代码将 tableHeaderView 固定在 ViewController 的屏幕上?我使用这段代码,但 tableViewHeader 在滚动时消失了:

import UIKit
class TestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    lazy var tableViewTest = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        createTable()
    }
    private func createTable() {
        self.tableViewTest = UITableView(frame: view.bounds, style: .grouped)
        tableViewTest.register(TestTableViewCell.self, forCellReuseIdentifier: "Test") 
        self.tableViewTest.delegate = self
        self.tableViewTest.dataSource = self
        tableViewTest.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        tableViewTest.separatorInset.left = 10
        tableViewTest.separatorInset.right = 10
        tableViewTest.tableHeaderView = "Test Header"
        view.addSubview(tableViewTest)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as! TestTableViewCell
        cell.testLabel.text = "test label"
        return cell
     }
}

这是我的第二个class:

import UIKit
class TestTableViewCell: UITableViewCell {
   let testLabel = UILabel()
   override func layoutSubviews() {
       super.layoutSubviews()
       testLabel.frame = CGRect(x: 60, y: 5, width: UIScreen.main.bounds.width - 80, height: 50)
            testLabel.numberOfLines = 0
            testLabel.sizeToFit()
            addSubview(testLabel)
       }
   override func awakeFromNib() {
        super.awakeFromNib()
   }
   override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
   }
}

谢谢。 这对我有用:

 import UIKit
    class TestViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
        lazy var tableViewTest = UITableView()
        var segmentedControl = UISegmentedControl(items: ["Test1", "Test2", "Test3"])
        override func viewDidLoad() {
            super.viewDidLoad()
            createTable()
            configureSegmentedControl()
        }
        private func createTable() {
            self.tableViewTest = UITableView(frame: view.bounds, style: .plain)
            tableViewTest.register(TestTableViewCell.self, forCellReuseIdentifier: "Test") 
            self.tableViewTest.delegate = self
            self.tableViewTest.dataSource = self
            tableViewTest.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            tableViewTest.separatorInset.left = 10
            tableViewTest.separatorInset.right = 10
            tableViewTest.tableFooterView = UIView()
            view.addSubview(tableViewTest)
        }
        
        private func configureSegmentedControl() {
            segmentedControl.selectedSegmentIndex = 0
            segmentedControl.frame = CGRect(x: 10, y: 150, width: UIScreen.main.bounds.width - 20.0, height: 40)
            segmentedControl.layer.cornerRadius = 5.0
            segmentedControl.backgroundColor = .blue
            segmentedControl.selectedSegmentTintColor = .red
            segmentedControl.tintColor = .white
            
            segmentedControl.addTarget(self, action: #selector(changeSegment), for: .valueChanged)
        }
        @objc func changeSegment(sender: UISegmentedControl) {
            switch sender.selectedSegmentIndex {
            case 0:
                print("Test1")
            case 1:
                print("Test2")
            default:
                print("Test3")
            }
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Test", for: indexPath) as! TestTableViewCell
            cell.testLabel.text = "test label"
            return cell
         }
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            return segmentedControl
        }
    
    }

尝试使用 UITableView 的隐式结构

lazy var tableViewTest = UITableView(frame: .zero, style: .plain)

但是您还需要将 table header 更改为您可以在 UITableViewDelegate 方法

中定义的 header 部分
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let identifier = YourSectionHeaderView.reuseIdentifier
        let headerView =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) 
        return headerView
    }

顺便说一句,我也建议你使用约束而不是自动调整大小掩码