UICollectionView在unhighlightAllItems上崩溃 [英] UICollectionView crash on unhighlightAllItems
问题描述
我收到了几个与iOS 7中的UICollectionView相关的崩溃报告。我无法一直重现这次崩溃。
I've gotten several crash reports related to a UICollectionView in iOS 7. I'm not able to consistently recreate this crash.
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x91c4392b
Crashed Thread: 0
Application Specific Information:
*** Terminating app due to uncaught exception '', reason: ''
Thread 0 Crashed:
0 libobjc.A.dylib 0x39dd2b26 objc_msgSend + 6
1 UIKit 0x31fd5eef -[UICollectionView cellForItemAtIndexPath:] + 111
2 UIKit 0x32060bfd -[UICollectionView _unhighlightItemAtIndexPath:animated:notifyDelegate:] + 149
3 UIKit 0x32383947 -[UICollectionView _unhighlightAllItems] + 151
4 UIKit 0x3205f9fb -[UICollectionView touchesBegan:withEvent:] + 367
5 UIKit 0x31fcb101 forwardTouchMethod + 233
6 UIKit 0x31fcb101 forwardTouchMethod + 233
7 UIKit 0x31e3be4b _UIGestureRecognizerUpdate + 5523
8 UIKit 0x31e73c41 -[UIWindow _sendGesturesForEvent:] + 773
9 UIKit 0x31e735e7 -[UIWindow sendEvent:] + 667
10 UIKit 0x31e48a25 -[UIApplication sendEvent:] + 197
11 UIKit 0x31e47221 _UIApplicationHandleEventQueue + 7097
12 CoreFoundation 0x2f69e18b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13 CoreFoundation 0x2f69d6e1 __CFRunLoopDoSources0 + 341
14 CoreFoundation 0x2f69be4f __CFRunLoopRun + 623
15 CoreFoundation 0x2f606ce7 CFRunLoopRunSpecific + 523
16 CoreFoundation 0x2f606acb CFRunLoopRunInMode + 107
17 GraphicsServices 0x342f4283 GSEventRunModal + 139
18 UIKit 0x31ea8a41 UIApplicationMain + 1137
19 JackThreadsIpad 0x000922b7 main (main.m:16)
应用程序中的UICollectionViewCells共享一个管理突出显示的公共超类。当单元格突出显示时,alpha会发生变化。
The UICollectionViewCells in the app share a common superclass that manages highlighting. When the cell is highlighted the alpha changes.
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.alpha = 0.8;
} else {
self.alpha = 1.0;
}
}
调用[super setHighlighted:highlight]会导致崩溃喜欢这个?该应用程序是使用XCode 4编译和提交的,并且仅在iOS 7上进行。任何其他建议以确定这种情况发生的位置。感谢您的帮助。
Could calling [super setHighlighted:highlighted] cause a crash like this? The app was compiled and submitted with XCode 4 and is only happening on iOS 7. Any other suggestions to figure out where this is happening. Thanks for your help.
编辑:
我能够在调试器中捕获到它,但它仍然不能始终如一地重现。崩溃是:
I was able to catch this in the debugger, but it still is not consistently reproducible. The crash is:
[NSIndexPath section] message sent to deallocated instance XXXXXXXX
推荐答案
如果在用户拖动视图时调用reloadData,可能就是原因。
If you are calling reloadData while the user is dragging the view, that might be the reason.
我遇到类似崩溃报告的崩溃,并通过延迟reloadData调用修复问题直到用户完成滚动视图。例如。创建一个包装方法,而不是直接调用reloadData。
I had crashes related to this with similar crash reports and "fixed" the issue by delaying the reloadData call until after the user has finished scrolling the view. E.g. create a wrapped method instead of calling reloadData directly.
- (void)updateData {
if (self.collectionView.isTracking) {
self.updateDataOnScrollingEnded = YES;
} else {
[self.collectionView reloadData];
}
}
然后当滚动结束时,调用updateData方法(如果滚动视图的委托方法。
Then when scrolling ends, call the updateData method (if needed) from the scroll view's delegate methods.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate) {
[self scrollViewStopped:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewStopped:scrollView];
}
- (void)scrollViewStopped:(UIScrollView *)scrollView
{
if (self.updateDataOnScrollingEnded) {
[self updateData];
self.updateDataOnScrollingEnded = NO;
}
}
我猜是有一个弱的引用在collectionView内部的某处突出显示了cell的indexPath,并且调用reload会释放该indexPath。当collectionView然后尝试取消突出显示单元格时,它会崩溃。
My guess is that there is a weak reference to the highlighted cell's indexPath somewhere inside of the collectionView, and that calling reload will dealloc that indexPath. When the collectionView then tries to unhighlight the cell, it crashes.
编辑:
如下面的评论所述,这个解决方案有一些缺陷。在进一步研究这个问题时,似乎在我的情况下,问题与在拖动集合视图期间在主线程上排队的多个reloadData调用有关。当只有一个reloadData调用时,一切都很好,但每当有多个 - 崩溃!
As mentioned in comments below, this "solution" has some flaws. While investigating the issue further, it seems that in my case the problem had to do with multiple reloadData calls being queued on the main thread during the dragging of the collection view. When there was only one reloadData call, everything was fine, but whenever there was more than one – crash!
因为我在我的collectionView中只有一个部分我替换了reloadData调用
Since I always had exactly one section in my collectionView i replaced the reloadData call with
reloadSections:[NSIndexSet indexSetWithIndex:0]
但是,这会导致细胞快速淡出并重新进入,我通过以下方法避免了这种情况(作为集合视图上的类别可能会更好)
However, this causes the cells to quickly fade out and back in again which I avoided with the following method (it would probably be better off as a category on the collection view)
- (void)reloadCollectionView:(UICollectionView *)collectionView animated:(BOOL)animated
{
[UIView setAnimationsEnabled:animated];
[collectionView performBatchUpdates:^{
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:^(BOOL finished) {
[UIView setAnimationsEnabled:YES];
}];
}
到目前为止,这对我来说效果很好,它也允许数据在滚动时实际更新。
So far, this has worked well for me and it also allows for the data to actually be updated while scrolling.
这篇关于UICollectionView在unhighlightAllItems上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!