【发布时间】:2018-04-11 09:53:28
【问题描述】:
我尝试根据本指南实现从 iOS10 开始呈现的内置 CollectionView 重新排序:http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/ 并且在 iOS11 上一切正常,但是在开始拖动 UICollectionView 中的任何项目(单元格)时在 iOS10 上崩溃
这是我的代码:
- (void)addCollectionViewGestureRecognizers
{
if (!self.panGesture)
{
// gesture to drag and reorder tiles
self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCollectionViewPanGesture:)];
[self.collectionView addGestureRecognizer:self.panGesture];
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
CLog(@"Item moved from index path: %@ to index path: %@", sourceIndexPath, destinationIndexPath);
}
- (void)handleCollectionViewPanGesture:(UIPanGestureRecognizer *)gesture
{
switch(gesture.state)
{
case UIGestureRecognizerStateBegan:
{
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
if(selectedIndexPath)
{
[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged:
{
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.collectionView endInteractiveMovement];
break;
}
default:
{
[self.collectionView cancelInteractiveMovement];
break;
}
}
}
和iOS 10每次上线都会崩溃:
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:guesture.view]];
崩溃报告:
0 CoreFoundation 0x0000000118f8bb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000118961141 objc_exception_throw + 48
2 CoreFoundation 0x0000000118ec0443 -[__NSArrayM insertObject:atIndex:] + 1603
3 UIKit 0x00000001166f3577 -[UICollectionView _getOriginalReorderingIndexPaths:targetIndexPaths:] + 501
4 UIKit 0x00000001166e2e50 -[UICollectionView _viewAnimationsForCurrentUpdate] + 7338
5 UIKit 0x00000001166e7bf3 __71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.2012 + 197
6 UIKit 0x0000000115e4c08e +[UIView(Animation) performWithoutAnimation:] + 90
7 UIKit 0x00000001166e682d -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3856
8 UIKit 0x00000001166e0b33 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 17030
9 UIKit 0x00000001166e91cd -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
10 UIKit 0x00000001166e9514 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 437
11 UIKit 0x00000001166e933c -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 91
12 UIKit 0x00000001166f09a3 -[UICollectionView _updateReorderingTargetPosition:forced:] + 1467
13 UIKit 0x00000001166f122a -[UICollectionView updateInteractiveMovementTargetPosition:] + 29
14 myAPP 0x000000010dcb3b26 -[MenuCollectionViewController handleCollectionViewPanGesture:] + 710
15 UIKit 0x00000001162c8f59 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
16 UIKit 0x00000001162d0d57 _UIGestureRecognizerSendTargetActions + 109
17 UIKit 0x00000001162ce70b _UIGestureRecognizerSendActions + 225
我忘了设置什么吗?
如果有人对此有任何想法,我将不胜感激......
【问题讨论】:
-
我认为应该是
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];注意不是self.view而是事实上gesture.view -
@staticVoidMan,对不起,那是我的错。当我复制和粘贴代码时,我错误地将 guesture.view 更改为 self.view。原始代码是[gesture locationInView:gesture.view],仅在iOS10上崩溃
-
@staticVoidMan,看来我找到了一个可以找到问题的地方。我有从 UICollectionViewFlowLayout 继承的自定义布局并覆盖其中的 targetIndexPathForInteractivelyMovingItem 函数。如果评论此覆盖,重新排序工作不会崩溃。只是项目移动到不正确的 indexPath
-
太棒了!您也应该将您的答案标记为已接受
标签: crash uicollectionview ios10.3