[英]ios NSIndexPath insertItemsAtIndexPaths in UICollectionView
[英]IOS UICollectionView insertItemsAtIndexPaths crash when header is on screen
我有一個UICollectionView
,標題大小非零。 似乎每當調用insertItemsAtIndexPaths
,如果標題在屏幕上,程序將崩潰並顯示以下消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: <_UICollectionViewItemKey: 0xa38c910> Type = SV Kind = UICollectionElementKindSectionHeader IndexPath = <NSIndexPath 0xa38c7c0> 2 indexes [0, 0])'
當標頭大小為零時,或者當標頭不在屏幕上時,調用insertItemsAtIndexPaths
可以正常工作。 有誰知道如何解決這一問題? 謝謝!
該類是UICollectionViewController的子類。 這是與UICollectionView相關的代碼:
- (id)init {
// Collection view layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100);
layout.headerReferenceSize = CGSizeMake(320, 50); // Left for the switch
layout.minimumInteritemSpacing = 5;
layout.minimumLineSpacing = 5;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
if (self = [super initWithCollectionViewLayout:layout]) {
// Collection view setup
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ID"];
self.collectionView.frame = CGRectMake(0, -20, 320, 480-20-44-49);
self.collectionView.backgroundView = nil;
self.collectionView.backgroundColor = [UIColor clearColor];
...
}
return self;
}
然后我實現了兩個委托方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [blogs count];
}
和
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewArg cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionViewArg dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
/* Some code to get blogView */
[cell.contentView addSubview:blogView];
return cell;
}
問題是在日志中說標題不能為零。所以在那里給出一些有效的輸入,你可以避免崩潰。
就像標題部分不需要輸入一樣,給它一個clearclolor視圖
要實現標頭部分,必須實現以下數據源方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
快樂編碼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.