Invalid update: invalid number of items in section 0 in my collection view
You never update your data model. Beside deleting the file and telling the collection view that an item has been deleted, you also need to update the model used by the collection view. This means you need to update numberOfRecordings
(before calling myCollectionView.deleteItems(at: [indexPath])
.
How to solve the error Invalid update: incorrect number of items in section 0 in the collection View on iOS12?
Where are the batch updates (plural)?
You don't need performBatchUpdates
, the method is only useful for multiple simultaneous updates. Delete the card in the database, on success remove the card from the data source array and delete the item – animated – in the collection view.
My suggestion assumes that the current index path is available which itemIndex
derives from
item = self.cards[itemIndex]
self.db.collection(K.FStore.collectionName!).document((item?.idCard)!).delete() { err in
if let error = err {
print("An error occurred", error)
} else {
self.cards.remove(at: itemIndex)
self.collectionView.deleteItems(at: [indexPath])
}
}
Invalid update: invalid number of items in section 0.
The call to insertItems(at:) and deleteItems(at:) must be accompanied with change in the datasource as well.
So, before calling these APIs, you would want to change your datasource, i.e. add objects into it before calling insertItems
and remove objects from it before calling deleteItems
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 items in section 0 in Reactive Programming issue
I found the problem it was while I try to change my array it goes to trouble. My solution is :
actionCollectionViewDidSelect.observeNext { [weak self] indexPath in
guard indexPath.row != 0 else { return }
guard let object = self?.arrayOfPhotoObjects[indexPath.row] else { return }
photoObject.isSelected = !photoObject.isSelected
if let value = self?.arrayOfObjects.value {
let collection = value.collection
let newArrayObject = MutableObservableArray<PhotoObject(collection)
self?.arrayOfPhotoObjects.value = newArrayObject.value }
}.dispose(in: disposeBag)
Invalid update: invalid number of items on UICollectionView
It's a bug with using insertItemsAtIndexPaths
on an empty UICollectionView
. Just do this:
if (self.birthdays.count == 1) {
[self.collectionView reloadData];
} else {
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem: (self.birthdays.count -1) inSection:0]]];
}
(Can't believe this is still broken in iOS8.)
How can I fix this Invalid update: invalid number of rows in section 0 error?
Your loop for creating the array of index paths is incorrect. You want it to iterate over data.count
, not items.count
. And you want the new index paths to be based on the previous items.count
.
private func addMoreRows(_ data: [Int]) {
var indexPaths = [IndexPath]()
for i in data.indices() {
indexPaths.append(IndexPath(row: items.count + i, section: 0))
}
self.items.append(contentsOf: data)
tableView.insertRows(at: indexPaths, with: .left)
}
Related Topics
Reference as Key in Swift Dictionary
How to Detect When Url of Amp Page Changed with Wkwebview
How to Use Raycast Methods in Realitykit
Rotate an Object in Its Direction of Motion
How to Play Multiple Audio Files Simultaneously Using Avplayer
Convert String to Bool in Swift - via API or Most Swift-Like Approach
Method' Is Ambiguous for Type Lookup in This Context, Error in Alamofire
Expected Hexadecimal Code in Braces After Unicode Escape
Swift Combine Sink Stops Receiving Values After First Error
Check If Class Conforms to Protocol
Trying to Stream Audio from Microphone to Another Phone via Multipeer Connectivity
How to Drag and Drop a Sprite in Swift 3.0
Swift: Second Occurrence with Indexof
Bool Being Seen as Int When Using Anyobject
How to Capture Depth Data from Camera in iOS 11 and Swift 4