Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 703
gumpwang Avatar asked Jan 28 '26 23:01

gumpwang


1 Answers

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.

like image 180
Barkley Avatar answered Jan 31 '26 12:01

Barkley