2015-05-29 170 views
4

我在我的應用上使用自定義UICollectionView。但是,我有一個問題,使我的應用程序崩潰。在日誌中崩潰:
注意:此錯誤不會在iOS 8UICollectionView收到ios中的單元格的佈局屬性7

發生
2015-05-29 11:49:26.316 app9to5[1834:607] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x7fca0e00> {length = 2, path 
= 0 - 14}' 

> First throw call stack: ( 
     CoreFoundation      0x039b61e4 __exceptionPreprocess + 180 
     libobjc.A.dylib      0x036808e5 objc_exception_throw + 44 
     CoreFoundation      0x039b6048 +[NSException raise:format:arguments:] + 136 
     Foundation       0x016954de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
     UIKit        0x02be0866 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1793 
     UIKit        0x02bdfaed -[UICollectionViewData validateLayoutInRect:] + 1376 
     UIKit        0x02ba5603 -[UICollectionView layoutSubviews] + 173 
     UIKit        0x025c8964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355 
     libobjc.A.dylib 

請幫我解決。非常感謝!

+0

你可以給我一些你的代碼,尤其是在你的佈局屬性設置功能,您的CollectionView委託? –

+0

@EugeneNguyen:您可以在鏈接https://gist.github.com/cuong-nguyen-ta/5b7f73f2fb9a316ffcf5查看此代碼 我認爲發生在我打電話時 '[self.collectionView reloadData];' –

+0

@EugeneNguyen這是功能設置佈局屬性 https://gist.github.com/cuong-nguyen-ta/22f6d7c5039d3743a4f1 請幫我解決。非常感謝! –

回答

3

你應該試試這個:

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect你把這個:

for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) { 
    for (NSInteger j=0 ; j < [self.collectionView numberOfItemsInSection:i]; j++) { 
     NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i]; 
     [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; 
    } 
} 

希望這可以幫助。

+0

沒錯。感謝Nghia的回答。 –

+0

@Nghia你正在返回所有的屬性,不只是屬性相交的指定矩形 – andrewz

+0

你讓我的日子....感謝:愛::愛::愛: –

3

在集合視圖上調用reloadData之前,嘗試使佈局無效。

[self.collectionView.collectionViewLayout invalidateLayout]; 
[self.collectionView reloadData]; 
+0

我試過 '[self.collectionView.collectionViewLayout invalidateLayout];' 但是,它不起作用。 @@ 感謝您的評論。 –

+0

完美運作。很多答案建議我創建一個新的collectionviewlayout,說實話這似乎過分殺死了。這個答案適用於我使用2個集合視圖。 thx – Tom

1

或者你也可以做到這一點

override func viewWillLayoutSubviews() { 
    super.viewWillLayoutSubviews() 
    self.collectionView.collectionViewLayout.invalidateLayout() 
} 
相關問題