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:

  1. Compares old and new data using Identifiable IDs
  2. Calculates the minimal set of changes
  3. Animates insertions, deletions, and moves
  4. 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 TypeAnimation
InsertionsNew items slide in smoothly
DeletionsRemoved items slide out
MovesReordered items animate to new positions
Cell UpdatesChanged values update in place

Scroll Position Preservation

Unlike the deprecated reload() method, setData preserves scroll position:

MethodScroll 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.