ios – unhighlightAllItems上的UICollectionView崩溃

前端之家收集整理的这篇文章主要介绍了ios – unhighlightAllItems上的UICollectionView崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到了几个与iOS 7中的UICollectionView相关的崩溃报告.我无法一致地重新创建此崩溃.
  1. Exception Type: SIGSEGV
  2. Exception Codes: SEGV_ACCERR at 0x91c4392b
  3. Crashed Thread: 0
  4.  
  5. Application Specific Information:
  6. *** Terminating app due to uncaught exception '',reason: ''
  7.  
  8. Thread 0 Crashed:
  9. 0 libobjc.A.dylib 0x39dd2b26 objc_msgSend + 6
  10. 1 UIKit 0x31fd5eef -[UICollectionView cellForItemAtIndexPath:] + 111
  11. 2 UIKit 0x32060bfd -[UICollectionView _unhighlightItemAtIndexPath:animated:notifyDelegate:] + 149
  12. 3 UIKit 0x32383947 -[UICollectionView _unhighlightAllItems] + 151
  13. 4 UIKit 0x3205f9fb -[UICollectionView touchesBegan:withEvent:] + 367
  14. 5 UIKit 0x31fcb101 forwardTouchMethod + 233
  15. 6 UIKit 0x31fcb101 forwardTouchMethod + 233
  16. 7 UIKit 0x31e3be4b _UIGestureRecognizerUpdate + 5523
  17. 8 UIKit 0x31e73c41 -[UIWindow _sendGesturesForEvent:] + 773
  18. 9 UIKit 0x31e735e7 -[UIWindow sendEvent:] + 667
  19. 10 UIKit 0x31e48a25 -[UIApplication sendEvent:] + 197
  20. 11 UIKit 0x31e47221 _UIApplicationHandleEventQueue + 7097
  21. 12 CoreFoundation 0x2f69e18b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
  22. 13 CoreFoundation 0x2f69d6e1 __CFRunLoopDoSources0 + 341
  23. 14 CoreFoundation 0x2f69be4f __CFRunLoopRun + 623
  24. 15 CoreFoundation 0x2f606ce7 CFRunLoopRunSpecific + 523
  25. 16 CoreFoundation 0x2f606acb CFRunLoopRunInMode + 107
  26. 17 GraphicsServices 0x342f4283 GSEventRunModal + 139
  27. 18 UIKit 0x31ea8a41 UIApplicationMain + 1137
  28. 19 JackThreadsIpad 0x000922b7 main (main.m:16)

应用程序中的UICollectionViewCells共享一个管理突出显示的公共超类.当单元格突出显示时,alpha会发生变化.

  1. - (void)setHighlighted:(BOOL)highlighted {
  2. [super setHighlighted:highlighted];
  3.  
  4. if (highlighted) {
  5. self.alpha = 0.8;
  6. } else {
  7. self.alpha = 1.0;
  8. }
  9. }

可以调用[super setHighlighted:highlight]引起这样的崩溃吗?该应用程序是使用XCode 4编译和提交的,并且仅在iOS 7上进行.任何其他建议以确定这种情况发生的位置.谢谢你的帮助.

编辑:
我能够在调试器中捕获它,但它仍然不能始终如一地重现.崩溃是:

  1. [NSIndexPath section] message sent to deallocated instance XXXXXXXX

解决方法

如果在用户拖动视图时调用reloadData,则可能是原因.

我使用类似的崩溃报告进行了与此相关的崩溃,并通过延迟reloadData调用来“解决”问题,直到用户完成滚动视图.例如.创建一个包装方法,而不是直接调用reloadData.

  1. - (void)updateData {
  2. if (self.collectionView.isTracking) {
  3. self.updateDataOnScrollingEnded = YES;
  4. } else {
  5. [self.collectionView reloadData];
  6. }
  7. }

然后当滚动结束时,从滚动视图的委托方法调用updateData方法(如果需要).

  1. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  2. {
  3. if (!decelerate) {
  4. [self scrollViewStopped:scrollView];
  5. }
  6. }
  7.  
  8. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  9. {
  10. [self scrollViewStopped:scrollView];
  11. }
  12.  
  13. - (void)scrollViewStopped:(UIScrollView *)scrollView
  14. {
  15. if (self.updateDataOnScrollingEnded) {
  16. [self updateData];
  17. self.updateDataOnScrollingEnded = NO;
  18. }
  19. }

我的猜测是在collectionView内部的某个突出显示的单元格的indexPath中有一个弱引用,并且调用reload会释放该indexPath.当collectionView尝试取消突出显示单元格时,它会崩溃.

编辑:

如下面的评论所述,这种“解决方案”存在一些缺陷.在进一步研究这个问题时,似乎在我的情况下,问题与在拖动集合视图期间在主线程上排队的多个reloadData调用有关.当只有一个reloadData调用时,一切都很好,但只要有多个 – 崩溃!

因为我总是在我的collectionView中只有一个部分,所以用.替换了reloadData调用

  1. reloadSections:[NSIndexSet indexSetWithIndex:0]

但是,这会导致细胞快速淡出并再次返回,我通过以下方法避免了这种情况(作为集合视图上的类别可能会更好)

  1. - (void)reloadCollectionView:(UICollectionView *)collectionView animated:(BOOL)animated
  2. {
  3. [UIView setAnimationsEnabled:animated];
  4. [collectionView performBatchUpdates:^{
  5. [collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
  6. } completion:^(BOOL finished) {
  7. [UIView setAnimationsEnabled:YES];
  8. }];
  9. }

到目前为止,这对我来说效果很好,它还允许在滚动时实际更新数据.

猜你在找的iOS相关文章