ios - 为什么第一次滚动到另一个 UITableView 中的 UICollectionView 中的 UITableView 不加载数据?
问题描述
我有一个UITableView
,其中一个单元格包含一个UICollectionView
. 然后, 的每个单元格UICollectionView
也包含一个UITableView
. 为了更清楚地说明这一点:
我第一次滚动到这个单元格时,什么都没有加载,单元格的高度只是 0。当我继续滚动外部 UITableView 直到这个特定的单元格离开屏幕(被破坏)并回到那里时,数据就是加载。这是一个简化的代码片段:
class ViewController: UIViewController {
@IBOutlet weak var outerTableView: UITableView!
func tableView(..., cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == ... {
// return regularCells
}
let cellThatHasCollectionView: CustomTableViewCell = ...
cellThatHasCollectionView.sectionData = ... // An array of array
return cellThatHasCollectionView
}
}
特殊UITableViewCell
定义如下:
private class CustomTableViewCell: UITableViewCell {
private let someCollectionView: UICollectionView = ...
public var sectionData: [[SomeType]] = []
init(...) {
...
someCollectionView.delegate = self
someCollectionView.dataSource = self
}
func collectionView(..., numberOfItemsInSection section: Int) -> Int {
return sectionData.count
}
func collectionView(..., cellForItemAt indexPath: IndexPath) -> ... {
let cell: CustomCollectionViewCell = ...
cell.items = sectionData[indexPath.row]
return cell
}
}
最后,我将这个内部UITableView
定义如下:
private class CustomCollectionViewCell: UICollectionViewCell {
public var items: [SomeType] = []
private let innerTableView: UITableView = ...
init(...) {
...
innerTableView.delegate = self
innerTableView.dataSource = self
}
func tableView(..., numberOfItemsInSection section) -> Int {
return items.count
}
...
}
我应该如何解决这个问题?:)
解决方案
public var sectionData: [[SomeType]] = []{
didSet{
someCollectionView.reloadData()
}
}
public var items: [SomeType] = []{
didSet{
innerTableView.reloadData()
}
}
您应该创建一个标志变量来控制someCollectionView
and innerTableView
,它只是在您第一次设置sectionData
and时加载items
推荐阅读
- postgresql - PG中如何获取物化视图的创建进度
- c++ - C++ 在全局范围内对变量使用静态是否只会将其更改为文件范围?
- node.js - 需要一种方法来增加/减少 Firebase 存储中目录中文件的计数器
- amazon-web-services - 企业级 CI/CD 管道的范围
- c++ - 用递归函数求解布尔方程
- c# - SharePoint 2019 基于自定义表单的身份验证使用活动目录“未与您共享的网站”
- python - 在 Windows 上的 Python Popen 子进程中暂停 FFmpeg 编码
- c - 具有 0 个子子树的级别顺序二叉树插入
- python - Diestel-Leader 图的绘图球
- python - 通过迭代行的函数运行 df