- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 Collection View ,我必须动态更改 Collection View 的布局。在设置新布局时,应用程序崩溃并显示日志,
2016-02-15 13:47:31.663 PhotoBoard[92347:5235792] *** 断言失败 -[UICollectionViewData layoutAttributesForItemAtIndexPath:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit -3512.30.14/UICollectionViewData.m:666
2016-02-15 13:47:31.666 PhotoBoard[92347:5235792] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-layoutAttributesForItemAtIndexPath 没有 UICollectionViewLayoutAttributes 实例:{length = 2,path = 0 - 0}'* 首先抛出调用栈:( 0 CoreFoundation 0x000000010d94de65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010f8b5deb objc_exception_throw + 48 2 CoreFoundation 0x000000010d94dcca +[NSException 引发:格式:参数:] + 106 3 基金会 0x000000010dfbe4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 UIKit 0x000000010ec4161c -[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 627 5 UIKit 0x000000010ebeee24 -[UICollectionView _setCollectionViewLayout:animated:isInteractive:completion:] + 1606 6 UIKit 0x000000010ebee4db -[UICollectionView setCollectionViewLayout:] + 318
在打印布局属性时,我看到了一个布局属性项layoutAttributesForItemAtIndexPath: {length = 2, path = 0 - 0}',我在布局中看到一个条目,所以不确定为什么找不到布局属性,
属性索引路径:({length = 2, path = 0 - 0});框架 = (6 293; 298 403.695);
属性索引路径:( {length = 2, path = 0 - 1});框架 = (6 293; 298 408.695);
属性索引路径:( {length = 2, path = 0 - 2});框架 = (6 293; 298 418.695);
属性索引路径:( {length = 2, path = 0 - 3});框架 = (6 293; 298 433.695);
属性索引路径:( {length = 2, path = 0 - 4});框架 = (6 293; 298 453.695);
属性索引路径:( {length = 2, path = 0 - 5});框架 = (6 293; 298 478.695);
属性索引路径:( {length = 2, path = 0 - 6});框架 = (6 293; 298 508.695);
属性索引路径:( {length = 2, path = 0 - 7});框架 = (6 293; 298 543.695);
属性索引路径:( {length = 2, path = 0 - 8});框架 = (6 293; 298 583.695);
属性索引路径:( {length = 2, path = 0 - 9});框架 = (6 293; 298 628.695);
属性索引路径:( {length = 2, path = 0 - 10});框架 = (6 293; 298 678.695);
属性索引路径:( {length = 2, path = 0 - 11});框架 = (6 293; 298 733.695);
属性索引路径:( {length = 2, path = 0 - 12});框架 = (6 293; 298 793.695);
属性索引路径:( {length = 2, path = 0 - 13});框架 = (6 293; 298 858.695);
属性索引路径:( {length = 2, path = 0 - 14});框架 = (6 293; 298 928.695);
属性索引路径:( {length = 2, path = 0 - 15});框架 = (6 293; 298 1003.69);
这就是我设置新的自定义布局的方式,
self.collectionView!.collectionViewLayout = self.stackLayout
self.stackLayout.delegate = self
self.collectionView!.collectionViewLayout.invalidateLayout()
self.collectionView!.setCollectionViewLayout(self.stackLayout, animated:true)
self.collectionView?.reloadData()
如果我静态加载两个自定义布局,它们工作正常,只有在动态更改布局时才会发生崩溃。
最佳答案
我遇到了同样的问题,这解决了我的问题。
如果您使用多个自定义布局进行切换,则在两个自定义布局类中重写以下方法,
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
Swift 示例:
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
objective-c 的示例:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.cache[indexPath.item];
}
编码愉快..!
关于swift - 使用 setCollectionViewLayout 导致崩溃,说找不到 layoutAttributesForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35419903/
我有这个(layout是UICollectionViewFlowLayout子类): [layout invalidateLayout]; [self.collectionView setCollec
我通过移动单元格来更新 UICollectionView 的布局。 collectionView.moveItem(at: sourceIndex, to: destIndex) collection
我有两个自定义布局的 collectionView,如果发生某些操作,我将从数据源中删除两个项目并尝试设置不同的 Collection View [self.tabCollectionView
如果我更改 UICollectionView 的布局在方向更改期间,我在旋转几次后得到 EXC_BAD_ACCESS。 - (void)willRotateToInterfaceOrientation
我有一个 Collection View ,我在其中使用自定义 UICollectionViewFlowLayout。此 Collection View 可以在本地或通过互联网加载图像。如果我在本地加
我的 UICollectionView 出现奇怪的崩溃。崩溃的 UICollectionView 嵌入到另一个 UICollectionView 的 UICollectionView 单元中。 我无法
我正在尝试在 之间转换我的收藏 View 水平胶片布局和 垂直堆栈布局。 水平膜条 垂直/扩展堆栈 这就是我目前正在做的事情: 使用 setCollectionViewLayout(_:animate
这个把我难住了。 func updateCollectionView() { DispatchQueue.main.async { [weak self] in if let
我有两个 nib 文件,分别是 RowRecordLayout 和 ColumnLayout。 我确实将 nib 单元格注册到 UICollectionView,以便稍后在 cellForItemAt
我有一个 Collection View ,我必须动态更改 Collection View 的布局。在设置新布局时,应用程序崩溃并显示日志, 2016-02-15 13:47:31.663 Photo
我正在使用具有两个自定义布局的 UICollectionView - 它们是 UICollectionViewFlowLayout 的子类 当我的 View 首次加载时,我使用以下方法将其设置为默认
您好,我正在尝试使用 UICollection View 并让它们具有多个布局。 当我使用 swift/obj-cs setCollectionViewLayout(toLayout, animate
我正忙于 iOS7 中的 UICollectionView。 我要在两种不同的布局之间更改我的 Collection View 的布局。它们是 UICollectionViewFlowLayout 的
我是一名优秀的程序员,十分优秀!