Ios 拖放;删除UICollectionView单元重用问题
因此,我为UICollectionView实现了如下拖放:Ios 拖放;删除UICollectionView单元重用问题,ios,swift,uicollectionview,drag-and-drop,Ios,Swift,Uicollectionview,Drag And Drop,因此,我为UICollectionView实现了如下拖放: func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { guard shouldAllowDragForIndexPath?(indexPath) == true else { return
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
guard shouldAllowDragForIndexPath?(indexPath) == true else { return [] }
guard let cell = collectionView.cellForItem(at: indexPath)?.toImage() else {
return []
}
let provider = NSItemProvider(object: cell)
let item = UIDragItem(itemProvider: provider)
item.localObject = data[indexPath.row]
return [item]
}
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
guard
let item = coordinator.items.first?.dragItem,
let sourceIndexPath = coordinator.items.first?.sourceIndexPath,
let destinationIndexPath = coordinator.destinationIndexPath,
let entity = item.localObject as? T
else {
return
}
collectionView.performBatchUpdates({ [unowned self] in
dropEntityAtIndexPath?(entity, destinationIndexPath)
self.collectionView.deleteItems(at: [sourceIndexPath])
self.collectionView.insertItems(at: [destinationIndexPath])
}, completion: nil)
coordinator.drop(item, toItemAt: destinationIndexPath)
item.localObject = nil
}
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if
session.localDragSession != nil,
let path = destinationIndexPath,
shouldAllowDropForIndexPath?(path) == true {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
} else {
return UICollectionViewDropProposal(operation: .forbidden)
}
}
一切正常,但我发现当细胞被重复使用时有奇怪的行为。
以下是视频:
所以它只发生在单元格被拖动之后,而且看起来有一些灰色的叠加视图,但我在视图调试器上看不到它。此外,我还尝试在prepareforeuse
中设置单元格的imageView.image=nil
,但没有效果。如果有人帮我解决这个问题,我将非常感激。谢谢
Edit1:在cellForItemAt
中编码:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: PhotoCollectionViewCell.self)
let container = data[indexPath.row].postContainer,
let post = container.postMedia.first
let isCarousel = container.isCarousel
let isVideo = post.isVideo
let _isSelected = isSelected?(indexPath) ?? false
let url = post.thumbURL?.toURL()
let isScheduled = container.schedulerFor != nil
let fromInstagram = false
cell.configure(url: url, isSelected: _isSelected, isVideo: isVideo, isCarousel: isCarousel, isScheduled: isScheduled, fromInstagram: fromInstagram)
return cell
}
编辑2
单元配置功能:
func configure(url: URL?, isSelected: Bool = false, isVideo: Bool = false, isCarousel: Bool = false, isScheduled: Bool = false, fromInstagram: Bool = false) {
checkmarkImageView.isHidden = !isSelected
selectionOverlay.isHidden = !isSelected
isVideoImageView.isHidden = !isVideo
carouselImageView.isHidden = !isCarousel
cornerImageView.image = isScheduled ? R.image.cyanCorner() : R.image.grayCorner()
instagramImageView.isHidden = !fromInstagram
cornerImageView.isHidden = fromInstagram
url.map(imageView.setImage(with:))
}
我认为问题不是单元重用,而是模型更新不当<代码>项。本地数据应在移动完成后正确更新 这意味着您必须在调用
UICollectionView
update之前更新数据模型对象
以下是UICollectionViewDelegate
的示例,但实际上只有这几行很重要:
// YOUR MODEL OBJECT is expected to be a list-like data structure
YOUR_MODEL_OBJECT.insert(YOUR_MODEL_OBJECT.remove(at: sourceIndex), at: destinationIndex)
它如何适用于UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
guard destinationIndexPath != sourceIndexPath else { return }
// YOUR MODEL OBJECT is expected to be a list-like data structure
YOUR_MODEL_OBJECT.insert(YOUR_MODEL_OBJECT.remove(at: sourceIndex), at: destinationIndex)
}
您的
cellForItem(at:)
函数是什么样子的?@JacobRelkin添加到问题中……您的配置函数是什么样子的?@JacobRelkin添加到问题中。@JacobRelkin如果以前发生过拖放,则总是在滚动之后。谢谢您的回答,我已经尝试了您的解决方案,但它对我没有帮助:(