我在使用 UICollectionView 时遇到如下错误:
2013-11-29 17:29:07.832 Clients[34200:70b]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** setObjectForKey: object cannot be nil
(key: <_UICollectionViewItemKey: 0x8da14f0>
Type = DV ReuseID = (null) IndexPath = (null))'
代码如下,我基本上有两个 NSMutableArrays -- 一个包含所有条目,另一个用于将条目减少到一个部分 -- 在这一点上,我将所有缺失的部分添加回 (希望这是有道理的)。
[self.collectionView performBatchUpdates:^{
NSString *selectedMarketSector = [[[[_marketSectors objectAtIndex:0] clients] objectAtIndex:0] clientMarketSector];
NSMutableIndexSet *insertSections = [[NSMutableIndexSet alloc] init];
for (TBMarketSector* sector in _allMarketSectors) {
if (![[[[sector clients] objectAtIndex:0] clientMarketSector ] isEqualToString:selectedMarketSector]) {
[insertSections addIndex:[_allMarketSectors indexOfObject:sector]];
}
}
_marketSectors = [self.allMarketSectors mutableCopy];
[self.collectionView insertSections:insertSections];
} completion:^(BOOL finished) {
[self setLayoutStyle:TBLayoutStacks animated:YES];
}];
“似乎”导致问题的行是 [self.collectionView insertSections:insertSections]; 但我想知道其他地方是否发生了一些奇怪的事情...
感谢您提供的任何帮助。
最佳答案
信不信由你,似乎这个特定问题实际上与我不想要的 UICollectionElementKindSectionFooter 有关。
我应该提到这只是 iOS7 的问题(这也不是我在切换到 iOS 7 后遇到的第一个 Collection View 问题)。
无论如何,我的自定义 UICollectionViewFlowLayout 实现了 - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 方法并返回一些属性所有补充意见。
要解决这个特殊问题,我需要添加以下内容:
if ([kind isKindOfClass:[UICollectionElementKindSectionFooter class]])
return nil;
谢谢。
关于ios - UICollectionView insertSections 错误 - NSInvalidArgumentException 原因 setObjectForKey 对象不能为 nil UICollectionViewItemKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290997/