Animated Updates
Update your table data with smooth, scroll-preserving animations.
How It Works
SwiftDataTables uses automatic diffing to animate data changes. When you call setData(_:animatingDifferences:), the table:
- Compares old and new data using
IdentifiableIDs - Calculates the minimal set of changes
- Animates insertions, deletions, and moves
- Updates changed cells in place
Basic Usage
var items: [Item] = []
var dataTable: SwiftDataTable!
func refresh() {
items = fetchNewItems()
dataTable.setData(items, animatingDifferences: true)
}What Gets Animated
| Change Type | Animation |
|---|---|
| Insertions | New items slide in smoothly |
| Deletions | Removed items slide out |
| Moves | Reordered items animate to new positions |
| Cell Updates | Changed values update in place |
Scroll Position Preservation
Unlike the deprecated reload() method, setData preserves scroll position:
| Method | Scroll Behavior |
|---|---|
| reload() | Resets to top |
| setData(..., animatingDifferences: false) | Preserves position |
| setData(..., animatingDifferences: true) | Preserves position + animates |
Common Patterns
Pull to Refresh
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
Task {
let newItems = await api.fetchItems()
await MainActor.run {
items = newItems
dataTable.setData(items, animatingDifferences: true) {
refreshControl.endRefreshing()
}
}
}
}Optimistic Updates
func deleteItem(_ item: Item) {
// Immediately remove from UI
items.removeAll { $0.id == item.id }
dataTable.setData(items, animatingDifferences: true)
// Then sync with server
Task {
do {
try await api.delete(item)
} catch {
// Rollback on failure
items.append(item)
dataTable.setData(items, animatingDifferences: true)
showError(error)
}
}
}Performance Considerations
For very large changes (1000+ rows), consider skipping animation: dataTable.setData(items, animatingDifferences: false)
Troubleshooting
Animations not working? Ensure your model conforms to Identifiable and IDs are stable between updates.