UICollectionView animate data change
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Animating data changes in a UICollectionView is less about visual polish and more about keeping the UI and data source perfectly synchronized. If your backing array and collection-view updates drift apart, you get “invalid update” crashes instead of smooth insertions, deletions, and moves.
The Basic Rule: Update Data and View Together
A collection view does not store your items for you. It asks the data source how many items exist and which cell belongs at each index path. Because of that, animation code must reflect the same change in both places.
Suppose you have a simple list of strings:
If you want to insert a new item at the beginning, update the array and then tell the collection view about the matching insertion.
Animating Inserts, Deletes, and Moves
The standard API is performBatchUpdates.
Deleting works the same way:
And moving an item requires both the array mutation and the collection-view move:
The animation comes from the collection view understanding exactly what changed.
When to Use reloadItems and When Not To
If the number of items stays the same and only the contents of one cell change, use reloadItems(at:) instead of deleting and reinserting.
This keeps the structure stable and only refreshes the affected cell.
What you should avoid for small changes is reloadData(). It refreshes everything and skips the fine-grained animation you usually want.
Diffable Data Source Is Usually Simpler
For modern UIKit code, UICollectionViewDiffableDataSource is often the cleanest solution. Instead of manually calling insert, delete, and move APIs, you build a snapshot and apply it. UIKit computes the differences and animates them for you.
This approach removes a lot of manual bookkeeping and is especially useful when multiple inserts and deletes happen at once.
Choosing Between the Two Approaches
Manual performBatchUpdates works well when:
- you already manage a small local array
- changes are simple and explicit
- you want direct control over the exact operations
Diffable data source works well when:
- the screen has more complex update logic
- items move often
- you want UIKit to compute the differences safely
Both are valid. The main requirement is consistency between your model state and the visual updates.
Common Pitfalls
The most common crash is updating the collection view without updating the data source, or updating them in a mismatched way. If the view thinks an item was inserted but numberOfItemsInSection still returns the old count, UIKit will throw an invalid update exception.
Another mistake is using reloadData() after a carefully planned batch update. That often cancels the point of the animation and can make the UI feel jumpy.
Layout changes can also affect animations. If your cell sizes depend on dynamic content, make sure the layout invalidation behavior matches the update pattern, or cells may animate awkwardly.
Finally, run UI updates on the main thread. Collection view mutations from background work can cause subtle crashes and inconsistent animation timing.
Summary
- '
UICollectionViewanimations work only when the data source and UI updates stay in sync.' - Use
performBatchUpdatesfor inserts, deletes, moves, and targeted reloads. - Update the backing data to match the exact collection-view operation.
- Prefer diffable data source for more complex or frequent state changes.
- Avoid
reloadData()when you want smooth, fine-grained animations.
Related reading
- UICollectionView auto scroll to cell at IndexPath
- UICollectionView current visible cell index
- UICollectionView flowLayout not wrapping cells correctly
- UICollectionView, full width cells, allow autolayout dynamic height?
- UICollectionView inside a UITableViewCell -- dynamic height?
- UICollectionView must be initialized with a non-nil layout parameter
- UICollectionView reloadData not functioning properly in iOS 7
- UICollectionView Self Sizing Cells with Auto Layout
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.