UICollectionView:组合布局禁用预取?

Ger*_*eon 9 prefetch ios uicollectionview swift

我有一个非常简单的方法UICollectionView,它使用组合布局来轻松实现动态单元格高度。不幸的是,这样做似乎禁用了使用UICollectionViewDataSourcePrefetching. 在以下示例代码中,该collectionView(_:prefetchItemsAt:)方法仅在初始显示集合视图时调用一次。没有滚动操作会导致对方法的进一步调用。

我该怎么做才能使预取工作?

class ViewController: UIViewController, 
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDataSourcePrefetching
{
    @IBOutlet var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.collectionViewLayout = createLayout()
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.prefetchDataSource = self
        collectionView.register(MyCell.self, forCellWithReuseIdentifier: "cell")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell
        cell.label.text = String(repeating: "\(indexPath) ", count: indexPath.item)

        return cell
    }

    // this is only called once. Why?
    func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
        print("prefetch for \(indexPaths)")
    }

    private func createLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout { (_, _) -> NSCollectionLayoutSection? in

            let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                              heightDimension: .estimated(44))
            let item = NSCollectionLayoutItem(layoutSize: size)

            let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitem: item, count: 1)
            group.interItemSpacing = .fixed(16)

            let section = NSCollectionLayoutSection(group: group)
            section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
            section.interGroupSpacing = 8

            return section
        }

        return layout
    }
}

class MyCell: UICollectionViewCell {
    let label = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)

        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        backgroundColor = .orange
        contentView.addSubview(label)

        label.translatesAutoresizingMaskIntoConstraints = false
        label.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
Run Code Online (Sandbox Code Playgroud)

(使用 Xcode 11.5 / iOS 13.5。collectionView插座连接到故事板中的全屏实例)

编辑:进一步的测试表明这heightDimension: .estimated(44)似乎是原因。将其替换为.absolute(44)使预取再次工作,但这当然会破坏具有多行文本的布局的目的。似乎是一个错误,如果有人想欺骗它,我已经提交了 FB7849272。

编辑/2:目前,我可以通过使用普通的旧流程布局并计算每个单独的单元格高度来避免这种情况,这也使预取再次工作。尽管如此,我很好奇在仍然使用组合布局的同时是否没有解决方法,所以我添加了一个赏金。

Ger*_*eon 0

我刚刚收到 Apple 的反馈,要求我在 Xcode 13.1 中重新测试这个问题,你瞧,这个 bug 确实被修复了。