gpt4 book ai didi

ios - UICollectionView:组合布局禁用预取?

转载 作者:行者123 更新时间:2023-12-01 15:37:59 24 4
gpt4 key购买 nike

我有一个很简单的UICollectionView它使用组合布局轻松实现动态单元格高度。不幸的是,这样做似乎会禁用使用 UICollectionViewDataSourcePrefetching 的内容预取。 .在以下示例代码中,collectionView(_:prefetchItemsAt:)方法仅在 Collection View 的初始显示时调用一次。没有滚动 Action 会导致对该方法的进一步调用。
我该怎么做才能使预取工作?

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")
}

}
(使用 Xcode 11.5/iOS 13.5。 collectionView socket 连接到 Storyboard中的全屏实例)
编辑:进一步测试表明 heightDimension: .estimated(44)似乎是原因。将其替换为 .absolute(44)使预取再次起作用,但这当然违背了使用多行文本进行这种布局的目的。似乎是一个错误,如果有人想欺骗它,我已经提交了 FB7849272。
EDIT/2:目前,我可以通过使用普通的旧流布局并计算每个单独的单元格高度来避免这种情况,这也使预取再次工作。尽管如此,我很好奇在仍然使用组合布局的同时是否没有解决方法,所以我添加了一个赏金。

最佳答案

是的..!!没有办法对动态大小的单元格使用预取,您必须使用 collectionView willDisplay forItemAt为了它

关于ios - UICollectionView:组合布局禁用预取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62726919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com