UICollectionView reloadData 遇上 layoutAttributesForSupplementaryElementOfKind “crash”

来源:互联网 发布:淘宝耐克旗舰店是真的 编辑:程序博客网 时间:2024/06/09 04:43

crash log:


2015-12-20 15:48:46.076 iosclient[1993:46171] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'

*** First throw call stack:

(

0   CoreFoundation                      0x01841a84 __exceptionPreprocess + 180

1   libobjc.A.dylib                     0x00ec7e02 objc_exception_throw + 50

2   CoreFoundation                      0x0170dd23 -[__NSArrayM objectAtIndex:] + 243

3   CoreFoundation                      0x017867f8 -[NSArray objectAtIndexedSubscript:] + 40

4   UIKit                               0x02774673 -[UICollectionViewFlowLayout _frameForHeaderInSection:usingData:] + 74

5   UIKit                               0x0276dbc9 -[UICollectionViewFlowLayout layoutAttributesForHeaderInSection:usingData:] + 72

6   UIKit                               0x0276edcc -[UICollectionViewFlowLayout layoutAttributesForSupplementaryViewOfKind:atIndexPath:] + 228

7   UIKit                               0x02795f76 -[UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:] + 477

8   UIKit                               0x0273e68b -[UICollectionView layoutAttributesForSupplementaryElementOfKind:atIndexPath:] + 90

9   iosclient                           0x0022bb69 _TFE9iosclientCSo16UICollectionView14scrollToHeaderfS0_FSiT_ + 153

10  iosclient                           0x0022bd6f _TToFE9iosclientCSo16UICollectionView14scrollToHeaderfS0_FSiT_ + 47

11  iosclient                           0x0024d678 _TFC9iosclient21RoomMapViewController13onFinishEventfS0_FSiT_ + 824

12  iosclient                           0x00251615 _TTWC9iosclient21RoomMapViewControllerS_13EnentDelegateS_FS1_13onFinishEventuRq_S1__fq_FSiT_ + 53

13  iosclient                           0x00199363 _TFC9iosclient12EventHandler4donefS0_FSiT_ + 355

14  iosclient                           0x001991f1 _TFC9iosclient12EventHandler4donefS0_FT_T_ + 33

15  iosclient                           0x0025265b _TFFC9iosclient21RoomMapViewController26dogetRoomMapLeafByBranchIdFS0_FSdT_U_FTGSqCSo7NSError_GSqPSs9AnyObject___T_ + 779

16  iosclient                           0x002500cd _TPA__TFFC9iosclient21RoomMapViewController26dogetRoomMapLeafByBranchIdFS0_FSdT_U_FTGSqCSo7NSError_GSqPSs9AnyObject___T_ + 77

17  iosclient                           0x0022785f _TFFC9iosclient9WebClient16doGetRoomMapLeafFS0_FTSd14LastRefushTimeSd8callbackFTGSqCSo7NSError_GSqPSs9AnyObject___T__T_U_FTGSqS1__GSqPS2____T_ + 95

18  iosclient                           0x00222c57 _TPA__TFFC9iosclient9WebClient16doGetRoomMapLeafFS0_FTSd14LastRefushTimeSd8callbackFTGSqCSo7NSError_GSqPSs9AnyObject___T__T_U_FTGSqS1__GSqPS2____T_ + 87

19  iosclient                           0x0026b639 _TFFC9iosclient4Http8get_gzipFS0_FTSS5paramGSqGVSs10DictionarySSPSs9AnyObject___8callbackFTGSqCSo7NSError_GSqPS2____T__T_U_FTGSqCSo13NSURLResponse_GSqCSo6NSData_GSqS3___T_ + 905

20  iosclient                           0x0026a24d _TPA__TFFC9iosclient4Http8get_gzipFS0_FTSS5paramGSqGVSs10DictionarySSPSs9AnyObject___8callbackFTGSqCSo7NSError_GSqPS2____T__T_U_FTGSqCSo13NSURLResponse_GSqCSo6NSData_GSqS3___T_ + 125

21  iosclient                           0x0026b2a2 _TTRXFo_oGSqCSo13NSURLResponse_oGSqCSo6NSData_oGSqCSo7NSError__dT__XFdCb_dGSqS__dGSqS0__dGSqS1___dT__ + 114

22  CFNetwork                           0x03e8db62 __67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 177

23  Foundation                          0x00b74a6b __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 12

24  Foundation                          0x00a988ff -[NSBlockOperation main] + 108

25  Foundation                          0x00a78d84 -[__NSOperationInternal _start:] + 697

26  Foundation                          0x00a78ac4 -[NSOperation start] + 83

27  Foundation                          0x00a78902 __NSOQSchedule_f + 245

28  libdispatch.dylib                   0x0460b9cd _dispatch_client_callout + 14

29  libdispatch.dylib                   0x045f0f90 _dispatch_main_queue_callback_4CF + 910

30  CoreFoundation                      0x017935fe __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14

31  CoreFoundation                      0x017512f4 __CFRunLoopRun + 2356

32  CoreFoundation                      0x01750706 CFRunLoopRunSpecific + 470

33  CoreFoundation                      0x0175051b CFRunLoopRunInMode + 123

34  GraphicsServices                    0x050f6664 GSEventRunModal + 192

35  GraphicsServices                    0x050f64a1 GSEventRun + 104

36  UIKit                               0x01dd81eb UIApplicationMain + 160

37  iosclient                           0x0019db4c main + 140

38  libdyld.dylib                       0x04635a21 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb) 


遇到这种问题总是很困扰,两个都在主线程了,还会crash?!! 这个是多线程并发??!!

我的代码是这样的:

RoomStatusTable.reloadData()RoomStatusTable.scrollToHeader(self.scrollToHeaderRow!)
extension UICollectionView{    func scrollToHeader(section:Int){        let index = NSIndexPath(forRow: 0, inSection: section)        if let attr = layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: index){            let top = CGPointMake(0, attr.frame.origin.y - contentInset.top)            setContentOffset(top, animated: true)        }    }}
要做的事情也很简单,就是加载完,重新刷新的时候,滑动到指定的headerRow

解决方法是:

RoomStatusTable.reloadData()NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in        if(self.scrollToHeaderRow != nil){              self.RoomStatusTable.scrollToHeader(self.scrollToHeaderRow!)        }})

加入延时执行,延时到下一个应用程序的下一个主循环

0 0
原创粉丝点击