0

Подскажите, как можно закрепить 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)
    }
}

1 ответ 1

0

Следующий код делает это:

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

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.