0

我有一个集合视图,其中它的单元格根据图像方向、标签内容等动态调整大小。

这很完美,基本上是通过下面的流布局委托方法来实现的。

extension TimelineViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(
        _ collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAt indexPath: IndexPath
    ) -> CGSize {
        let resource = dataSource.items[indexPath.section].items[indexPath.row]
        resizingCell.model = resource
        let calculatedSize = resizingCell.contentView.systemLayoutSizeFitting(
            UILayoutFittingCompressedSize
        )
        return calculatedSize
    }
}

如果它有所作为,resizingCell上面是在我的viewDidLoad方法中定义的:

let identifier = String(describing: MyCell.self)
let cellNib = UINib(nibName: identifier, bundle: nil)
collectionView.register(cellNib, forCellWithReuseIdentifier: identifier)
resizingCell = cellNib.instantiate(withOwner: nil, options: nil).first as? MyCell

上面的问题是我的委托方法indexPath在我的集合视图中被调用,甚至在它显示在屏幕上之前。高度计算相对昂贵,当我的数据源中有超过 1000 个项目时,集合视图甚至可能需要几秒钟才能出现在屏幕上。

这是预期的行为吗?如果不是,我能做些什么吗?如果我必须预先计算每个单元的大小,这有点违背了可重复使用单元的目的。

感谢您查看这个。

4

0 回答 0