0
votes

I'm using collection view in side UITableViewCell, on reload

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let _cell = cell as? BrandRow else { return }

        let _flow = columnLayout
        _flow.scrollDirection = .horizontal

        _cell.collectionView.collectionViewLayout = _flow
        _cell.collectionView.delegate = self
        _cell.collectionView.tag = indexPath.section
        _cell.collectionView.dataSource = self
        _cell.collectionView.showsVerticalScrollIndicator = false
        _cell.collectionView.showsHorizontalScrollIndicator = false
        _cell.collectionView.reloadData()

    }

In iPhone 6 while cell get reused it crashes my app

_cell.collectionView.reloadData()

can anyone please let me know what's wrong with my code?

Error

Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.140/UICollectionViewData.m:447

2021-01-28 17:10:08.921104+0500 Okayhai[630:48362] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xec57a98e0a4dfaae> {length = 2, path = 0 - 4}'

1
The code that sets the layout, delegate and scroll indicators should be in another function that is only run once. What is the crash detail/exception message? - Paulw11
check the error i have mentioned in query. - Mudasir Syed
willDisplayCell is not a good place to be running this code. The one-time setup you should do in an init method. The other code should be in your cellForRowAt. Storing indexpath data in tag is also best avoided. It can cause problems if your table allows cell reordering or insertion/deletion. - Paulw11

1 Answers

0
votes

try this:
below _cell.collectionView.reloadData()
put

_cell.collectionView.collectionViewLayout.invalidateLayout()