markhorrocks
markhorrocks

Reputation: 1398

UICollectionView reloadData() crashes after emtying datasource dictionary

I have a UICollectionView which takes the results of an API search. The search is triggered by the following code. The results are appended to a dictionary [[String: Any]] and I call self.collectionView.reloadData() after my query completes.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    var newValue = textField.text!
    let location = min(range.location, newValue.characters.count)
    let startIndex = newValue.characters.index(newValue.startIndex, offsetBy: location)
    let endIndex = newValue.characters.index(newValue.startIndex, offsetBy: location + range.length)
    let newRangeValue = Range<String.Index>(startIndex ..< endIndex)
    newValue.replaceSubrange(newRangeValue, with: string)

    searchView.searchFieldValueChanged(newValue)

    return true
}

Then, if I want to change the search string and search again I want to empty the dictionary and call reloadData() again I get an app crash.

The error is

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist:

Here is my datasource implementation

var searchResults = [[String: Any]]()


    let layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        layout.estimatedItemSize.height = 200
        layout.estimatedItemSize.width = 200
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        return layout
    }()

    collectionView = UICollectionView(frame: self.frame, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(LanesCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    collectionView.backgroundColor = .yellow // Constants.APP_BACKGROUND_COLOR
    collectionView.alwaysBounceVertical = true
    collectionView.clipsToBounds = true
    collectionView.translatesAutoresizingMaskIntoConstraints = false

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if searchResults.count == 0 {
        collectionView.alpha = 0.0
    } else {
            collectionView.alpha = 1.0
    }
    return searchResults.count
}

after query

func parseMoreData(jsonData: [String: Any]) {

    let items = jsonData["items"] as! [[String: Any]]

        self.collectionView.layoutIfNeeded()

        self.collectionView.reloadData()
    }
}

func searchFieldValueChanged(_ textValue: String) {

    searchResults = []

Upvotes: 1

Views: 1816

Answers (1)

markhorrocks
markhorrocks

Reputation: 1398

This looks fixed by using this instead of layoutIfNeeded()

 collectionView.reloadData()

 collectionView.collectionViewLayout.invalidateLayout()

Upvotes: 2

Related Questions