swift – 使用setCollectionViewLayout导致崩溃,说找不到layoutA
我有一个集合视图,我必须动态更改集合视图的布局.在设置新布局时,应用程序与日志崩溃,
2016-02-15 13:47:31.663 PhotoBoard [92347:5235792] ***断言失败 – [UICollectionViewData layoutAttributesForItemAtIndexPath:],/ BuildRoot / Library / People / com.apple.xbs / Source / UIKit_Sim / UIKit-3512.30. 14 / UICollectionViewData.m:666 2016-02-15 13:47:31.666 PhotoBoard [92347:5235792] *由于未捕获的异常’NSInternalInconsistencyException’而终止应用程序,原因:’没有UICollectionViewLayoutAttributes -layoutAttributesForItemAtIndexPath实例:{length = 2,path = 0 – 0}’ 在打印布局属性时,我看到一个布局属性项 属性索引路径:({length = 2,path = 0 – 0}); frame =(6 293; 298 403.695); 属性索引路径:({length = 2,path = 0 – 1}); frame =(6 293; 298 408.695); 属性索引路径:({length = 2,path = 0 – 2}); frame =(6 293; 298 418.695); 属性索引路径:({length = 2,path = 0 – 3}); frame =(6 293; 298 433.695); 属性索引路径:({length = 2,path = 0 – 4}); frame =(6 293; 298 453.695); 属性索引路径:({length = 2,path = 0 – 5}); frame =(6 293; 298 478.695); 属性索引路径:({length = 2,path = 0 – 6}); frame =(6 293; 298 508.695); 属性索引路径:({length = 2,path = 0 – 7}); frame =(6 293; 298 543.695); 属性索引路径:({length = 2,path = 0 – 8}); frame =(6 293; 298 583.695); 属性索引路径:({length = 2,path = 0 – 9}); frame =(6 293; 298 628.695); 属性索引路径:({length = 2,path = 0 – 10}); frame =(6 293; 298 678.695); 属性索引路径:({length = 2,path = 0 – 11}); frame =(6 293; 298 733.695); 属性索引路径:({length = 2,path = 0 – 12}); frame =(6 293; 298 793.695); 属性索引路径:({length = 2,path = 0 – 13}); frame =(6 293; 298 858.695); 属性索引路径:({length = 2,path = 0 – 14}); frame =(6 293; 298 928.695); 属性索引路径:({length = 2,path = 0 – 15}); frame =(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] } 目标C的示例: - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { return self.cache[indexPath.item]; } 快乐编码..! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |