Invalid Update: Invalid Number of Sections in Uitableview

Invalid update: invalid number of sections

The error says it all. When if (self.states?.count)! > 3 is false. The only section would be inserted and not deleted.
You should update your data source accordingly. The number of sections method must return someArray.count. When you insert some section, make sure to update that some array, and when you delete some section, delete the element from some array. That will resolve the issue.

Invalid update: invalid number of sections when doing TableView.reloadData()

As mentioned, the number of inserted (1) + deleted (1) is 1 - 1 = 0, whereas you're returning the value 2 (someArray.count is 2). Did you use peformBatch? insert/delete rows?

Why does it happen? it's hard to know from the very short code example you've provided, I'm assuming your array is not thread safe (you've mentioned decoding from UserDefaults), therefore by the time numberOfSections is called, the data is not synchronised yet.

Try to make a simpler someArray not accessing UserDefaults, and make your code run without crashing.

Invalid update: invalid number of sections in UITableView

When you remove the last row from a section, you need to let the table view that the whole section has been removed.

You can do this by implementing

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, 
didChange sectionInfo: NSFetchedResultsSectionInfo,
atSectionIndex sectionIndex: Int,
for type: NSFetchedResultsChangeType) {

let section = IndexSet(integer: sectionIndex)

switch type {
case .delete:
tableView.deleteSections(section, with: .automatic)
case .insert:
tableView.insertSections(section, with: .automatic)
}
}

Invalid update: invalid number of rows in section 0 with NSFetchedResultsController

Doc states:

controller(_:didChange:at:for:newIndexPath:)

indexPath

The index path of the changed object (this value is nil for
insertions).

So, you should change the code like this:

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .none)
}
case .move:
if let indexPath = indexPath {
tableView.moveRow(at: indexPath, to: newIndexPath)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
default:
tableView.reloadData()
}
}
}

Invalid update: invalid number of rows in section UITableView problem

in my case I return const values

There you go! If you are going to modify the number of rows you need variables

For example

var sections = [3,4,2]

func numberOfSections(in _: UITableView) -> Int {
return sections.count
}

func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section]
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
sections[indexPath.section] += 1
tableView.insertRows(at: [IndexPath(row: 0, section: indexPath.section)], with: .left)
}


Related Topics



Leave a reply



Submit