iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist

假如想象 提交于 2019-11-27 10:36:22

This happened to me when number of cells in collectionView changed. Turns out I was missing invalidateLayout after calling reloadData. After adding it, I haven't experienced any more crashes. Apple has made some modifications to collectionViews in iOS10. I guess that's the reason why we are not experiencing same problem on older versions.

Here's my final code:

[self.collectionView reloadData];
[self.collectionView.collectionViewLayout invalidateLayout];
ArturC

When you have custom layout, remember to clear cache (UICollectionViewLayoutAttributes) while overriding prepare func

    override func prepare() {
       super.prepare()
       cache.removeAll()
    }

I also faced this issue with 2 collectionViews in the same view because I added the same UICollectionViewFlowLayout in both collections :

let layout = UICollectionViewFlowLayout()
collectionView1.collectionViewLayout = layout
collectionView2.collectionViewLayout = layout

Of course it crashes on reload data if collectionView1 Data change. If this could help someone.

Previous answer helps, but if you use autoresizing cells, their size will be incorrect.

 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 layout.estimatedItemSize = CGSizeMake(60, 25);
 layout.itemSize = UICollectionViewFlowLayoutAutomaticSize;             
 self.collectionView.collectionViewLayout = layout;

I solve this issues by replacing

[self.collectionView reloadData];

to

[self.collectionView reloadSections:indexSet];

The @Katrin's answer helped a lot, but I could achieve even better results by adding one more line:

collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutSubviews() // <-- here it is :)

I can't now say if I could reproduce crash with this line or not, but I guess there was one... So, still not a silver bullet, but something.

Calling invalidateLayout did not prevent the crash in my case. (It worked if the number of items in the collection view increased but not if it decreased). Context: I have a UICollectionView inside a UITableViewCell and when the table cell is re-used I reset the delegates of the collectionView. I fixed the problem not by invalidating the cache but by RECREATING the layout object any time I reset the delegate, then calling reloadData():

foo.dataSource = newDataSource
foo.delegate = newDelegate
foo.fixLayoutBug()
foo.reloadData()

func fixLayoutBug() {
    let oldLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let newLayout = UICollectionViewFlowLayout()
    newLayout.estimatedItemSize = oldLayout.estimatedItemSize
    newLayout.footerReferenceSize = oldLayout.footerReferenceSize
    newLayout.headerReferenceSize = oldLayout.headerReferenceSize
    newLayout.itemSize = oldLayout.itemSize
    newLayout.minimumInteritemSpacing = oldLayout.minimumInteritemSpacing
    newLayout.minimumLineSpacing = oldLayout.minimumLineSpacing
    newLayout.scrollDirection = oldLayout.scrollDirection
    newLayout.sectionFootersPinToVisibleBounds = oldLayout.sectionFootersPinToVisibleBounds
    newLayout.sectionHeadersPinToVisibleBounds = oldLayout.sectionHeadersPinToVisibleBounds
    newLayout.sectionInset = oldLayout.sectionInset
    newLayout.sectionInsetReference = oldLayout.sectionInsetReference
    collectionViewLayout = newLayout
}
Kevin Bel

It's not the best to reloadData everytime (You should use insertItems and deleteItems, and even reloadSections). But... after saying that in some cases it's a valid so, you can actually do this:

collectionView.dataSource = nil

collectionView.delegate = nil

/*... All changes here. ... */

collectionView.dataSource = self

collectionView.delegate = self

collectionView.reloadData()

In my case, I was changing the NSlayoutConstraint constant

self.collectionViewContacts.collectionViewLayout.invalidateLayout()

            UIView.animate(withDuration: 0.3) {
                self.view.layoutIfNeeded()
            }


            self.collectionViewContacts.reloadData()

solved the issue

Resetting UICollectionView's layout solved the problem for me:

let layout = UICollectionViewFlowLayout()
collectionView?.collectionViewLayout = layout

If you want to keep your scroll position and fix the crash you can use this:

collectionView.reloadData()
let context = collectionView.collectionViewLayout.invalidationContext(forBoundsChange: collectionView.bounds)
context.contentOffsetAdjustment = CGPoint.zero
collectionView.collectionViewLayout.invalidateLayout(with: context)

I had the problem using RxDataSources with RxCollectionViewSectionedAnimatedDataSource and solved it by combining two answers. I have to invalidateLayout() when reactively reload my collection:

    ...
    .asDriver(onErrorJustReturn: [])
    .do(onNext: { [weak self] _ in
        DispatchQueue.main.async {
            self?.collectionViewLayout.invalidateLayout()
            self?.collectionView.layoutSubviews()
        }
    }).drive(collectionView.rx.items(dataSource: collectionController.dataSource))
    .disposed(by: disposeBag)

and clear cache array(s) in prepare() layout method. Here is the code:

override func prepare() {
    super.prepare()
    cache.removeAll()
    guard let collectionView = collectionView,
        collectionView.numberOfSections > 0,
        let delegate = delegate else {
        return
    }
    ...

I had this problem as well. In my case there was a custom UICollectionViewLayout applied to the collection view & the problem was that it had stale data for the number of cells that should be displayed. That's definitely something you can check on when you see UICollectionView received layout attributes for a cell with an index path that does not exist

I managed to solve this by recreating the collectionViewLayout before calling reloadData()

The issue was probably related to me having a separate instance for the dataSource (i.e. not the view controller holding the collectionView), and when swapping datasource, the crash could appear.

This happened to me as well, but it was because my UICollectionViewDataSource changed, and I didn't call -[UICollectionView reloadData]. In my case, I had the following data structure:

struct Bar { let name: String }
struct Foo { let name: String; let bars: [Bar] }

I had two UICollectionViews: one for Foos and one for Bars. In my -collectionView:didSelectItemAtIndexPath:, I had the following:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  if (collectionView == self.fooCollectionView) {
    self.selectedFoo = self.foos[indexPath.row];
    [self.barCollectionView reloadData];
    self.fooCollectionView.hidden = YES;
    self.barCollectionView.hidden = NO;
  } else if (collectionView == self.barCollectionView) {
    // do some stuff with the selected bar

    self.selectedFoo = nil;
    // This -reloadData call fixed my error. I thought I didn't
    // need it since my UICollectionView was hidden
    [self.barCollectionView reloadData];
    self.barCollectionView.hidden = YES;
    self.fooCollectionView.hidden = NO;
  }
}

Without the -reloadData call, I would see the crash when I rotated the device.

After spending two days of time, the below code solved the issue. Before loading a collectionview write the below code.

let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
collecView.collectionViewLayout = flowLayout 
collecView.delegate = self
collecView.dataSource = self
collecView.reloadData()

Then the crash will be resolved, but you will find issue in collectionview cells, as the cells are compressed or not as per your design. Then just a line of code after scrolldirection line

flowLayout.itemSize = CGSize(width: 92, height: 100)

With the above line of code, you can adjust you collectionview cell layout.

I hope it will help someone.

In my case I was changing the constant of NSLayoutConstraint none of the above solution worked for me. thanks to @jeff ayan whose answer gave me the hint. I solved the problem with this code

override func prepareForReuse() {
    super.prepareForReuse()
    imagesCollectionHeight.constant = 100 // changing collectionview height constant
    imageContainerWidth.constant = 100  //changing width constant of some view
    self.layoutIfNeeded() // this line did the trick for me
}

Hope it helps someone

The following made it for me:

[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];

This code seems to force collectionView to scroll to the first cell before reloading data, what seems to cause the error in my case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!