我有一个 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
和 innerTableView
,当您设置 sectionData
和 项目
关于ios - 为什么我的 UITableView 在另一个 UITableView 中的 UICollectionView 中,在第一次滚动到它时不加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55603946/