I faced "Invalid update: Invalid number of rows" in iOS 16.4 alone when I added newly obtained data from a pagination API call to the data source and performed performBatchUpdates with empty updates closure to add the rows to my CollectionView. The issue persisted even after trying to update my data source within the updates closure of performBatchUpdates. I fixed this issue by manually creating indexpaths for the newly added data in datasource and appending the created indexpath array to the collectionView's index path. Here is the code for the same :-
extension UICollectionView {
func refreshCollectionView(from: Int, to: Int, completionHandler: (() -> Void)? = nil) {
if from > to {
print("'from' index is greater than 'to' index")
return
}
self.performBatchUpdates({ [weak self] in
if from < to {
var indexpathArray: [IndexPath] = []
for index in from...to {
let indexpath = IndexPath(row: index, section: 0)
indexpathArray.append(indexpath)
}
self?.insertItems(at: indexpathArray)
} else if from == to {
self?.insertItems(at: [IndexPath(row: from, section: 0)])
}
}, completion: {_ in
if let completionHandler = completionHandler {
completionHandler()
}
})
}
}
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: