【发布时间】:2020-11-09 19:44:21
【问题描述】:
当在具有contentOffset.x > 0 的水平滚动UICollectionView 上调用performBatchUpdates 时,它会执行一个相当奇怪的动画。
我使用示例单元格重新创建了错误,该单元格准确显示了我们在应用程序中遇到的错误。当performBatchUpdates 中根本没有要执行的更改时,也会发生这种情况。完成后,UICollectionView中的项目是正确的,所以只是动画问题。
是否有人已经遇到过这个问题并有解决方案?
我的ViewController.swift:
class CollectionViewCell: UICollectionViewCell {
@IBOutlet var label: UILabel!
}
class ViewController: UIViewController {
@IBOutlet private var collectionView: UICollectionView!
private var items: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 1, height: 1)
layout.scrollDirection = .horizontal
collectionView.collectionViewLayout = layout
collectionView.dataSource = self
collectionView.delegate = self
collectionView.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
collectionView.showsHorizontalScrollIndicator = false
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell
cell?.label.text = items[indexPath.row].description
return cell ?? UICollectionViewCell()
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
items.remove(at: indexPath.row)
collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: [indexPath])
}, completion: nil)
}
}
在此处下载示例项目: https://wetransfer.com/downloads/74454a6b9df114529ba3a26b7b9488ab20200720141429/05eb4d
【问题讨论】:
-
我下载了你的项目并运行它,首先发生的事情是一百个严重的警告被转储到控制台中。在您解决这些问题之前,绝对没有任何意义。您的单元格估计大小和自动布局约束管理不正确。
标签: ios swift xcode uicollectionview uikit