简体   繁体   English

当标题在屏幕上时,IOS UICollectionView insertItemsAtIndexPaths崩溃

[英]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.

相关问题 ios UICollectionView中的ios NSIndexPath insertItemsAtIndexPaths - ios NSIndexPath insertItemsAtIndexPaths in UICollectionView UICollectionView insertItemsAtIndexPaths:引发异常 - UICollectionView insertItemsAtIndexPaths: throws exception UICollectionView insertItemsAtIndexPaths引发NSInternalInconsistencyException - UICollectionView insertItemsAtIndexPaths Throws NSInternalInconsistencyException UICollectionview insertItemsAtIndexPaths 停止滚动 - UICollectionview insertItemsAtIndexPaths stops scrolling UICollectionView insertItemsAtIndexPaths:引发错误 - UICollectionView insertItemsAtIndexPaths: raising error 当添加项目(核心数据)时,更新UICollectionView(卡在insertItemsAtIndexPaths上) - Updating a UICollectionView when an item is added (Core Data), stuck on insertItemsAtIndexPaths 水平设置UICollectionView的标头时,应用程序崩溃 - Application crash when set header of UICollectionView horizontally UICollectionView和iOS 7导致SVPullToRefresh崩溃 - SVPullToRefresh crash with UICollectionView and iOS 7 iOS 7中的UICollectionView奇怪崩溃 - UICollectionView strange crash in iOS 7 UICollectionView insertItemsAtIndexPaths:似乎重新加载collectionview或自动滚动到顶部 - UICollectionView insertItemsAtIndexPaths: seems to reload collectionview or autoscrolls to top
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM