ios - 为什么我的 UITableView 在另一个 UITableView 中的 UICollectionView 中,在第一次滚动到它时不加载数据?

标签 ios swift uitableview uicollectionview

我有一个 UITableView,其中一个单元格包含一个 UICollectionView。然后,UICollectionView 的每个单元格还包含一个 UITableView。为了使这一点更清楚:

Check below Image

我第一次滚动到这个单元格时,没有加载任何内容,单元格的高度只是 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()
    }
}

您应该创建一个标志变量来控制 someCollectionViewinnerTableView,当您设置 sectionData项目

关于ios - 为什么我的 UITableView 在另一个 UITableView 中的 UICollectionView 中,在第一次滚动到它时不加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55603946/

相关文章:

ios - "Onesignal for Voip push"和 "Firebase Cloud Messaging"不能一起工作

arrays - 将大型 std::vector 导入 Swift 的最有效方法?

ios - 滚动 UITableView 以关闭 UIView Above

ios - UITableViewCell 错误 : this class is not key value coding-compliant

ios - 为我的计算器添加小数点

ios - tableView 单元格虚线边框未正确呈现

swift - UIAlertController 不会把后台的所有元素都转成B/W

ios - 自定义 CollectionViewCell 在选择时具有默认值

iphone - CSS - iphone中的绝对定位? (phonegap, jqtouch)

ios - 在 ios 应用程序中使用 facebook 应用程序登录在 safari "Login with Facebook app"中显示选项并且它在 ios 10.1 中不起作用