UICollectionView:组合布局禁用预取?
UICollectionView: compositional layout disables prefetching?
我有一个非常简单的 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")
}
}
(使用 Xcode 11.5 / iOS 13.5。collectionView
插座连接到故事板中的全屏实例)
编辑:进一步测试表明 heightDimension: .estimated(44)
似乎是原因。将其替换为 .absolute(44)
可以再次进行预取,但这当然会破坏具有多行文本布局的目的。似乎是一个错误,如果有人想欺骗它,我已经提交了 FB7849272。
EDIT/2:目前,我可以通过使用普通的旧流布局并计算每个单独的单元格高度来避免这种情况,这也使得预取再次工作。尽管如此,我很好奇在仍然使用组合布局的同时是否没有解决方法,所以我增加了赏金。
我遇到了和你一样的问题,但我设法解决了。
我的布局尺寸是按分数计算的,而不是估计的:
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalWidth(2/3)))
并且,在委托方法prefetchItemsAt 中,您可以使用indexPaths 来计算您需要在服务器上请求的偏移量。例如:
var model : [YourModel] = []
collectionView.dataSource = self
collectionView.prefetchDataSource = self
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model.count
}
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
self.loadMore(for: indexPaths)
}
func loadMore(for indexPaths: [IndexPath]) {
for index in indexPaths where index.row >= (self.model.count - 1) {
manager?.requestCharacters(withOffset: model.count, onSuccess: { [unowned self] (objects) in
self.model += objects
self.collectionView.reloadData()
}, onError: { (error) in
print(error.localizedDescription)
})
return
}
}
是的..!!没有办法对动态大小的单元格使用预取,你必须使用 collectionView willDisplay forItemAt
我刚刚收到 Apple 的反馈,要求我在 Xcode 13.1 中重新测试这个问题,你瞧,这个错误确实已经修复了。
我有一个非常简单的 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")
}
}
(使用 Xcode 11.5 / iOS 13.5。collectionView
插座连接到故事板中的全屏实例)
编辑:进一步测试表明 heightDimension: .estimated(44)
似乎是原因。将其替换为 .absolute(44)
可以再次进行预取,但这当然会破坏具有多行文本布局的目的。似乎是一个错误,如果有人想欺骗它,我已经提交了 FB7849272。
EDIT/2:目前,我可以通过使用普通的旧流布局并计算每个单独的单元格高度来避免这种情况,这也使得预取再次工作。尽管如此,我很好奇在仍然使用组合布局的同时是否没有解决方法,所以我增加了赏金。
我遇到了和你一样的问题,但我设法解决了。 我的布局尺寸是按分数计算的,而不是估计的:
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalWidth(2/3)))
并且,在委托方法prefetchItemsAt 中,您可以使用indexPaths 来计算您需要在服务器上请求的偏移量。例如:
var model : [YourModel] = []
collectionView.dataSource = self
collectionView.prefetchDataSource = self
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model.count
}
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
self.loadMore(for: indexPaths)
}
func loadMore(for indexPaths: [IndexPath]) {
for index in indexPaths where index.row >= (self.model.count - 1) {
manager?.requestCharacters(withOffset: model.count, onSuccess: { [unowned self] (objects) in
self.model += objects
self.collectionView.reloadData()
}, onError: { (error) in
print(error.localizedDescription)
})
return
}
}
是的..!!没有办法对动态大小的单元格使用预取,你必须使用 collectionView willDisplay forItemAt
我刚刚收到 Apple 的反馈,要求我在 Xcode 13.1 中重新测试这个问题,你瞧,这个错误确实已经修复了。