【问题标题】:How I can pin the tableHeaderView?如何固定 tableHeaderView?
【发布时间】:2020-12-01 14:31:17
【问题描述】:

请告诉我,如何通过代码将 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
     }
}

这是我的第二节课:

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

【问题讨论】:

  • 使用 UITableHeaderFooterView 并应用分组的 UITableView 样式。

标签: swift uitableview tableview


【解决方案1】:

尝试使用 UITableView 的隐式构造

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

但您还需要将表头更改为可以在 UITableViewDelegate 方法中定义的节头

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let identifier = YourSectionHeaderView.reuseIdentifier
        let headerView =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) 
        return headerView
    }

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

【讨论】:

  • 您好!谢谢!
【解决方案2】:

谢谢。 这对我有用:

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

【讨论】:

    猜你喜欢
    • 2017-01-04
    • 2014-04-05
    • 1970-01-01
    • 2021-10-31
    • 2011-06-19
    • 2023-04-04
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多