首页 > 解决方案 > 使用另一个滚动视图滚动表格视图,单元格未加载

问题描述

我对 UI 有一个奇怪的要求,需要使用嵌入页面视图的滚动视图在页面视图中滚动表格视图。例如:

import UIKit

class TableVC: UIViewController {

    let tableView = UITableView()
    var observer: NSKeyValueObservation?


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

    func setupTableView() {
        view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.heightAnchor.constraint(equalToConstant: 2000)
        ])


        tableView.dataSource = self
        tableView.delegate = self
        tableView.isScrollEnabled = false
        tableView.backgroundColor = .blue

        let handler = { (tableView: UITableView, change: NSKeyValueObservedChange<CGSize>) in
            if let contentSize = change.newValue {
                print("contentSize:", contentSize)
            }
        }
        observer = tableView.observe(\UITableView.contentSize, options: [NSKeyValueObservingOptions.new], changeHandler: handler)
    }

}

extension TableVC: UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        50.0
    }

}

extension TableVC: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 40
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.backgroundColor = .green
        return cell
    }

}

和主视图控制器:

import UIKit

class ViewController: UIViewController {

    private enum Constants {
        static let pageViewControllerOptions: [UIPageViewController.OptionsKey: Any] = [
            .interPageSpacing: CGFloat(8.0)
        ]
    }

    private let pageVC: UIPageViewController = UIPageViewController(
        transitionStyle: .scroll,
        navigationOrientation: .horizontal,
        options: Constants.pageViewControllerOptions
    )

    let scrollView: UIScrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(scrollView)
        scrollView.pin(to: view)

        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.frame = view.bounds
        stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        scrollView.addSubview(stackView)

        let blueView = UIView()
        blueView.backgroundColor = .blue
        stackView.addArrangedSubview(blueView)
        blueView.translatesAutoresizingMaskIntoConstraints = false
        blueView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true

        addChild(pageVC)
        pageVC.didMove(toParent: self)
        let pageView = pageVC.view!
        stackView.addArrangedSubview(pageView)
        pageVC.setViewControllers([TableVC()], direction: .forward, animated: false, completion: nil)

        // 50 height with 20 cells works fine
        // 50 height with 40 cells won't work beacuse it doesn't seem to load them all
        scrollView.contentSize = CGSize(width: view.bounds.width, height: 2000 + 100)

    }

}

有 40 个单元格,我预计内容大小为 2000,我在上面设置了这个(也允许蓝色视图的高度)。滚动有效,但似乎在某些时候它已经停止生成单元格:

在此处输入图像描述

在上面的 gif 中,您可以看到少于 40 个单元格。我试过使用更大的表格视图高度,调用layoutIfNeeded但结果是一样的。我能做些什么来解决这个问题吗?我想单元格没有加载,因为我没有使用表格视图自己的滚动视图,所以cellForRow没有被触发。有没有另一种方法来加载单元格?

标签: iosswiftuitableviewuiscrollview

解决方案


推荐阅读