Добавление двух UITableViews в один UIVIewController программным способом и желание изменить представление контроллера с помощью UISegmentedControl - PullRequest
0 голосов
/ 26 сентября 2019

The GIF Result of what the problem is Я хочу изменить представление моего ViewController на основе 2 tableVIews с segmentedControl.Я достиг этого с программным подходом.Я создал два TableViews и один SegmentedControl.Но когда я изменяю segmentedControl, он остается в том же индексе, но изменяя представление.Я должен дважды нажать на каждый сегмент, чтобы добраться до него.Почему это происходит?

Вот мой код:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureFirstTableView()
    }

    var firstTableView = UITableView()
    var secondTableView = UITableView()

    func configureFirstTableView() {
        firstTableView.frame = self.view.frame
        firstTableView.delegate = self
        firstTableView.dataSource = self

        firstTableView.register(UITableViewCell.self, forCellReuseIdentifier: "FirstCell")
        view.addSubview(firstTableView)
    }

    func configureSecondTableView() {
        secondTableView.frame = self.view.frame
        secondTableView.delegate = self
        secondTableView.dataSource = self

        secondTableView.register(UITableViewCell.self, forCellReuseIdentifier: "SecondCell")
        view.addSubview(secondTableView)
    }

    func configureSegmentedControl() -> UISegmentedControl {
        let segmentedControl = UISegmentedControl(frame: CGRect(x: 10, y: 5, width: view.frame.width - 10, height: 30))
        segmentedControl.insertSegment(withTitle: "First", at: 0, animated: false)
        segmentedControl.insertSegment(withTitle: "Second", at: 1, animated: false)
        segmentedControl.selectedSegmentIndex = 0

        segmentedControl.addTarget(self, action: #selector(changeSegmentedControlValue(_:)), for: .valueChanged)
        return segmentedControl
    }

    @objc func changeSegmentedControlValue(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            print("1")
            configureFirstTableView()
        case 1:
            print("2")
            configureSecondTableView()
        default:
            break
        }
    }

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var count: Int?
        if tableView == firstTableView {
            count = 3
            return count!
        } else if tableView == secondTableView {
            count = 4
            return count!
        }

        return count!
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == firstTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath)
            cell.textLabel?.text = "First"
            return cell
        } else if tableView == secondTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
            cell.textLabel?.text = "Second"
            return cell
        }
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = .white
        v.addSubview(configureSegmentedControl())
        return v
    }

}

...