gumpwang
gumpwang

Reputation: 109

UICollection crash Invalid update: invalid number of sections.

The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).

Upvotes: 3

Views: 4086

Answers (1)

Barkley
Barkley

Reputation: 6713

Are you basing your numberOfSections off a model of some sort? What's happening is you're inserting a section into your table view somewhere, reloading your table view data, and then the app is crashing because numberOfSections does not equal The number of sections contained in the collection view after the update.

Without seeing your code it's hard to pinpoint where the problem is, but I would do a look for where you're inserting a section and figure out how to update your numberOfSections accordingly.

A basic example would be having a stored variable like

var sectionsCount = 1

and when you insert a section, you can do sectionsCount = sectionsCount + 1 , reload your tableView data, and have :

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionsCount.count
    }

Remember you always have to update your model that you're basing your table view off of BEFORE you reload the data.

Upvotes: 2

Related Questions