【发布时间】:2020-06-14 04:17:01
【问题描述】:
所以我已经为我的 UICollectionView 实现了拖放,如下所示:
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)
}
}
一切正常,但我发现重复使用单元格时出现了奇怪的行为。 这是视频:link
所以它只发生在拖动单元格之后,而且似乎有一些灰色的覆盖视图,但我在视图调试器上看不到它。另外,我尝试将单元格的imageView.image = nil 设置为prepareForReuse,但没有帮助。如果有人帮我解决这个问题,我将不胜感激。谢谢。
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:))
}
【问题讨论】:
-
你的
cellForItem(at:)函数是什么样的? -
@JacobRelkin 已添加到问题中。
-
...你的
configure函数是什么样的? -
@JacobRelkin 已添加到问题中。
-
@JacobRelkin 如果之前发生拖放,则始终在滚动之后。
标签: ios swift uicollectionview drag-and-drop