[英]IOS UICollectionView insertItemsAtIndexPaths crash when header is on screen
I have an UICollectionView
with a header of size non-zero. 我有一个
UICollectionView
,标题大小非零。 It seems whenever insertItemsAtIndexPaths
is called, if the header is on screen, the program crashes with the following message: 似乎每当调用
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])'
When the header size is zero, or when the header is not on screen, calling insertItemsAtIndexPaths
works fine. 当标头大小为零时,或者当标头不在屏幕上时,调用
insertItemsAtIndexPaths
可以正常工作。 Does anyone know how to fix this? 有谁知道如何解决这一问题? Thanks!
谢谢!
The class is a sub class of UICollectionViewController. 该类是UICollectionViewController的子类。 Here is the code related to UICollectionView:
这是与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;
}
Then I implemented two delegate methods: 然后我实现了两个委托方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [blogs count];
}
and 和
- (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;
}
The problem is there said in log that the header cannot be nil.So give some valid input there and you can avoid the crash. 问题是在日志中说标题不能为零。所以在那里给出一些有效的输入,你可以避免崩溃。
Like if the header section needs no input give it an view with clearclolor 就像标题部分不需要输入一样,给它一个clearclolor视图
For implementhing the header section you must implement the following datasource method 要实现标头部分,必须实现以下数据源方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
Happy coding :) 快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.