我需要使用moveItem(at:to:)委托方法来移动我的集合视图单元格。当我实现数据源委托方法时,它工作得很好,但是我需要将我的数据源设置为UICollectionViewDiffableDataSource,这样moveItem(at:to:)就不会调用。任何需要处理的解决方案,
在使用委托时,它是这样工作的:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
cell.textLabel.text = "Some value"
return cell
}
override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
true
}
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print("OK")
}以这种方式设置数据源不起作用
func setupDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, Person>(collectionView: collectionView, cellProvider: {
(collectionView: UICollectionView, indexPath: IndexPath, person: Person) -> UICollectionViewCell? in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
cell.textLabel.text = "Some text"
return cell
})
}发布于 2020-03-10 13:40:30
对于自定义行为,创建UICollectionViewDiffableDataSource的子类并覆盖那里的方法。
https://stackoverflow.com/questions/60611991
复制相似问题