Invalid update: invalid number of items on UICollectionView — How to solve this bug?
Hi everybody!
In this article I am going to share with you possible solution for a common bug that exists in UICollectionView.
But before, let me ask you my usual question:
Did you read my previous articles? 🤔 If not, then here they are: 👀
- Important things that every iOS Developer should know + Book advise
- What is Reactive Programming | RxSwift + Bonus Books
- Async/await in Swift | What is it?!
- What is SwiftLint? — The trip to the Clean Code
- Grand Central Dispatch & DispatchQueue in Swift
- What is SOLID?! — Bonus DRY principle
- (Possible) Interview questions and answers for every junior iOS Developers
- 13 Git commands that you will need in your career — Cheat Sheet
- What is Lifecycle of ViewController? — iOS Development with Swift
- How to become a developer? || Why you should NOT become?
- Reference and Value types in Swift — What are they?
- Unit testing | Swift
- Codable — Encodable/Decodable
- 7 sources to learn Swift
- What is Objective-C?
- 14 profiles that you MUST follow if you wanna become an iOS Dev.
- How to support “Dark Mode” in an iOS App — Swift, SwiftUI & Objective-C
- 15 Interview-questions that you will be asked as an iOS Developer
- How to create an AI chatbot using Swift? — Thanks for 100 followers!
- Swift | Inheritance & Overriding
- What is CI/CD? | Continuos Integrity & Delivery
- Avoid this 10 common mistakes as an iOS Developer
- What is SwiftUI — Does it worth to learn it?
- UIKit & SwiftUI — Which one to choose: A Comparative Analysis for iOS Development
Ok, now let’s start with our main topic. 🧐
Developers — all of us hate bugs, but keep in mind that if there is no bugs, then there is no work.
In iOS Development one of the most common bug is “Invalid update: invalid number of items on UICollectionView”.
All of us have seen this bug at least for once. If you did not see it yet, don’t worry, you will see it one day. 😉
I cannot show you wrong implementation here, because all of us have different way of implementing code, but if you faced with this bug, then here is one possible solution for you, thank me later. 😎
- Introduce a new variable:
var someNewVariable: Int = 0
2. Create following function || edit it:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return someNewVariable
}
3. Call this appropriate place in your code:
override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
updateItems.forEach { ourItem in
switch ourItem.updateAction {
case .add:
someInsertedIndex.append((item.indexPathBeforeUpdate, item.indexPathAfterUpdate))
someNewVariable += 1 // We define addition assignment operator.
case .delete:
someDeletedIndex.append((item.indexPathBeforeUpdate, item.indexPathAfterUpdate))
someNewVariable -= 1 // We define addition assignment operator.
default:
break
}
}
}
That will solve your problem. The third code block is just an example, please keep in mind that you should understand the logic and implement it properly in your project.
For further reading, at the very end of my article you will find useful links. 🙂
Yes, you might say that introducing new variable might create new bugs or it is a workaround — I respect your idea but I just disagree.
Oh you want to thank me: You have couple of options: clap, comment, share, follow or buy me a coffee. 🙂
Don’t forget to follow us on twitter. 🥳
Further reading:
https://developer.apple.com/documentation/uikit/uicollectionviewlayout/1617784-prepare
https://developer.apple.com/documentation/uikit/uicollectionviewupdateitem