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.)
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 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)
}
}
Related Topics
Swift 3.0, Alamofire 4.0 Extra Argument 'Method' in Call
No Such File and Directory Found Xcode 7
How to Use Charles Proxy on the Xcode 6 (iOS 8) Simulator
Macos on Vmware Doesn't Recognize iOS Device
Sending Push Notifications to iOS from Pwa
Bringing a Subview to Be in Front of All Other Views
Align Text Using Drawinrect:Withattributes:
Where Are iOS Simulator Screenshots Stored
Icon Already Includes Gloss Effects
Xcode Instruments Allocations: Look at Live Bytes or Overall Bytes
Xcode & Swift - Detecting User Touch of Uiview Inside of Uiscrollview
How to Expand Uicollectionview Contentsize When Paging Enable
How to Set Fake Gps Location on iOS Real Device
Uitableview:Viewforheaderinsection: Not Called During Reloaddata: