---
title: "#206: UITableView Cookbook 🍳📖"
url: https://littlebitesofcocoa.com/206-uitableview-cookbook
author: Jake Marsh
published: 2016-03-07
topics: ["UIKit","Techniques"]
description: "Today we'll check out a few different tips and tricks for working with UITableView. Let's get started. We can add pull-to-refresh capabilities to any..."
---

# #206: UITableView Cookbook 🍳📖

By Jake Marsh, March 7, 2016. HTML: https://littlebitesofcocoa.com/206-uitableview-cookbook

Today we'll check out a few different tips and tricks for working with _UITableView_. Let's get started.

## Adding a Refresh Control

We can add **pull-to-refresh** capabilities to any _UITableView_ easily:

``` swift
let refreshControl = UIRefreshControl()
refreshControl.addTarget(
  self, 
  action: Selector("refreshed:"), 
  forControlEvents: .ValueChanged
)

tableView.addSubview(refreshControl)
```

Then we just implement that `refreshed:` **function** like this:

``` swift
func refreshed(sender: UIRefreshControl) {
  refreshTheThing {
    sender.endRefreshing()
  }
}
```

## Reload Visible Cells

Here's a quick (almost) one-liner to reload <i>only</i> the **visible cells** in the **table view**:

``` swift
tableView.reloadRowsAtIndexPaths(
  tableView.indexPathsForVisibleRows ?? [],
  withRowAnimation: .Automatic
)
```

## Accessing Rects

It's often handy to grab a reference to the actual **frame** of a **cell**, **section**, **header** or **footer**. We can do this easily with:

``` swift
tableView.rectForRowAtIndexPath(
  selectedIndexPath
)
```

## Animate Height Changes

One easy trick to make our **app** look a little nicer is to animate changes to the heights of the **UITableViewCells** in our _UITableView_. All we need to do is make whatever changes to our models we want to cause our cells to be a different height. (For example we might let the user tap a **button** to <i>"expand"</i> all <i>comments</i> in an **app**). Then we just call:

``` swift
tableView.beginUpdates()
tableView.endUpdates()
```

**UIKit** will interpolate & animate the changes, neat!
