UICollectionView收到ios 7

时间:2015-05-29 07:31:02

标签: ios objective-c ios7 uicollectionview

我在我的应用上使用自定义UICollectionView。但是,我有一个问题,使我的应用程序崩溃。崩溃的日志:
注意:此错误不会在iOS 8上发生

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

请帮我解决。非常感谢你!

3 个答案:

答案 0 :(得分:3)

在集合View上调用reloadData之前尝试使布局无效。

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

答案 1 :(得分: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]];
    }
}

希望这可以提供帮助。

答案 2 :(得分:2)

或者你可以这样做

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.collectionView.collectionViewLayout.invalidateLayout()
}