ios - 使用另一个滚动视图滚动表格视图,单元格未加载
问题描述
我对 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
没有被触发。有没有另一种方法来加载单元格?
解决方案
推荐阅读
- python - 不同维度张量的乘法
- python - 我是否正确理解了在 python lib“线程”中的获取和释放是如何工作的
- c - 使用指针访问二维数组的元素
- linux - 如何使用 exec 在 find 命令中添加其他参数?
- python - 在 tkinter 画布上绘制一条粗曲线
- ios - iOS 设备上的 Cordova 部署失败:找不到 Info.plist 文件?
- postgresql - PostgreSQL - 删除有条件的值
- flutter - 杂注代码块必须放在颤振的主类中吗
- r - 适用于数据框并删除列索引
- android - 使用特定比例分配模式升级/降级订阅时出现“错误”