如何解决UICollectionView 拖放在单元格之间放置时会崩溃
在我的项目中,我正在一个简单的 UIcollectionView 上实现拖放功能:1 列,1 节。为了实现这一点,我遵循了 this 教程。
悬停单元格时它工作得很好,但是如果我尝试将拖动的单元格放在两个其他单元格之间,我会崩溃。
在我的布局中,我添加了 40 的 minimumLineSpacing
,经过一些挖掘,我意识到如果我走得太快并到达两个之间的空间,我会得到 nil
的 destinationIndexPath
单元格。
- (UICollectionViewDropProposal *)collectionView:(UICollectionView *)collectionView dropSessionDidUpdate:(id<UIDropSession>)session withDestinationIndexPath:(NSIndexPath *)destinationIndexPath {
// `destinationIndexPath` is `nil` when hovering the space between two cells
if (collectionView.hasActiveDrag)
return [[UICollectionViewDropProposal alloc] initWithDropOperation:UIDropOperationMove intent:UICollectionViewDropIntentInsertAtDestinationIndexPath];
return [[UICollectionViewDropProposal alloc] initWithDropOperation:UIDropOperationForbidden];
}
当我到达 - (void)collectionView:performDropWithCoordinator:
时,indexPath 为零,因此在尝试执行 collectionview 更新时崩溃。
- (void)collectionView:(UICollectionView *)collectionView performDropWithCoordinator:(id<UICollectionViewDropCoordinator>)coordinator {
if (coordinator.proposal.operation != UIDropOperationMove)
return;
NSIndexPath *indexPath = coordinator.destinationIndexPath
// ^ this is sometimes nil
// ...
}
有没有人遇到过这个问题?
即使在由 interItemSpacing 分隔的两个单元格之间,我如何告诉 collectionView 计算它的 destinationIndexPath
?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。