首页 > 解决方案 > 在 UITableViewCell 内使用 UICollectionView 滚动滞后(闪烁)

问题描述

我已经实现了一个类似 UI 的 Appstore/Netflix,其中有一个 UITableView,并且在每个(自定义)单元格内都有一个水平滚动的 UICollectionView。

每个 UITableView 单元格都充当其 collectionView 的委托和数据源。数据由 tableView 单元格内的变量保存,该变量在 tableView 的 cellForRow 调用期间设置。

设置新变量后,didSet 执行一些操作并调用 collectionView.ReloadData() 以使 collectionView 反映当前数据。这会导致滚动时出现延迟,可能是因为每个 collectionView.ReloadData() 调用。

数据是一个结构,其中包含一些元数据和内容本身在一个数组中。

cellForRow:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: contentTableCellId, for: indexPath) as! ContentTableCell
    cell.contentItem = contentItems[indexPath.row]
    return cell
}

在 ContentTableCell 内部:

    var contentItem: ContentItem!{
    didSet{
        ... //some simple text label actions.
        self.collectionView.reloadData()
    }
}

标签: iosswiftuitableviewuicollectionviewreloaddata

解决方案


推荐阅读