iOS 10 bug: UICollectionView received layout attri

2019-01-10 05:45发布

Running my app in a device with iOS 10 I get this error:

UICollectionView received layout attributes for a cell with an index path that does not exist

In iOS 8 and 9 works fine. I have been researching and I have found that is something related to invalidate the collection view layout. I tried to implement that solution with no success, so I would like to ask for direct help. This is my hierarchy view:

->Table view 
    ->Each cell of table is a custom collection view [GitHub Repo][1]
        ->Each item of collection view has another collection view

What I have tried is to insert

    [self.collectionView.collectionViewLayout invalidateLayout];

In the

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

of both collection views.

Also I have tried to invalidate layout before doing a reload data, does not work...

Could anyone give me some directions to take?

14条回答
Lonely孤独者°
2楼-- · 2019-01-10 06:30

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.

查看更多
贼婆χ
3楼-- · 2019-01-10 06:33

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)
查看更多
forever°为你锁心
4楼-- · 2019-01-10 06:40

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.

查看更多
Juvenile、少年°
5楼-- · 2019-01-10 06:41

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.

查看更多
三岁会撩人
6楼-- · 2019-01-10 06:42

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

    override func prepare() {
       super.prepare()
       cache.removeAll()
    }
查看更多
女痞
7楼-- · 2019-01-10 06:43

Resetting UICollectionView's layout solved the problem for me:

let layout = UICollectionViewFlowLayout()
collectionView?.collectionViewLayout = layout
查看更多
登录 后发表回答