2015-05-29 168 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() 
} 
相关问题