---
title: "#272: Automatic Table View \u0026 Collection View Animations with Dwifft 🚶🏼🏃🏼"
url: https://littlebitesofcocoa.com/272-automatic-table-view-collection-view-animations-with-dwifft
author: Jake Marsh
date: 2016-10-04
published: 2016-10-04
topics: ["UIKit","Libraries"]
description: "UITableView \u0026 UICollectionView have some fantastic animation capabilities. We can animate items and sections being inserted, removed, moved, etc."
---

# #272: Automatic Table View & Collection View Animations with Dwifft 🚶🏼🏃🏼

By Jake Marsh, October 4, 2016. HTML: https://littlebitesofcocoa.com/272-automatic-table-view-collection-view-animations-with-dwifft

_UITableView_ & _UICollectionView_ have some fantastic animation capabilities. We can animate **items** and **sections** being inserted, removed, moved, etc. We've all been there though, `reloadData` is sitting right there, and is conceptually so simple. Wouldn't it be great if we could enable those fancy animations in our views, with the conceptually simplicity of `reloadData`?

Today we'll check out [Dwifft](https://github.com/jflinter/Dwifft), a library from [Jack Flintermann](https://github.com/jflinter) that can help us achieve this. Let's dive in.

We'll begin with a regular _UITableViewController_. (Dwifft works just as well on _UICollectionViews_).

First we'll need a `TableViewDiffCalculator`. This is a generic type, so we'll tell it that we're going to be display a bunch of `Strings`.

``` swift
class ExampleViewController: UITableViewController {
  var diffCalculator: TableViewDiffCalculator<String>?
```

Next we'll initialize our data with some random items (just a randomly ordered **array** of `Strings`). We'll also use a **property observer** to let our _diff calculator_ know whenever our **items** change.

``` swift
var items: [String] = ExampleViewController.randomItems() {
  didSet { self.diffCalculator?.rows = items }
}
```

We're almost there. We'll initialize the _diff calculator_ in our **view controller's** `viewDidLoad` **function**:

``` swift
override func viewDidLoad() {
  super.viewDidLoad()

  tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ItemCell")

  diffCalculator = TableViewDiffCalculator<String>(
    tableView: tableView,
    initialRows: items
  )
}
```

Lastly, we'll implement a couple of standard `UITableViewDataSource` functions:

``` swift
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
  cell.textLabel?.text = selfitems[(indexPath as NSIndexPath).row]
  return cell
}
```

Success! Here's how this ends up looking:

<img src="https://media.littlebitesofcocoa.com/272-1.gif" style="max-width: 400px;" />

The <i>"Shuffle"</i> button was wired up off-camera, but it simply sets `self.items = ExampleViewController.randomItems()`.

Now we can simply update the **diff calculator**'s rows and we'll get all those <i>beautiful</i>, scroll-position preserving animations <i>automatically</i>.

A couple of final notes, we can customize the individual **animation types** like this:

``` swift
diffCalculator?.insertionAnimation = .fade
diffCalculator?.deletionAnimation = .fade
```

And finally for fun the science behind [Dwifft](https://github.com/jflinter/Dwifft) is pretty neat to read about. [Read more about the Longest Common Subsequence Problem here](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem).

Learn more about [Dwifft](https://github.com/jflinter/Dwifft) at [git.io/dwifft](http://git.io/dwifft)

<p></p>
