Menu & Search

How to properly do buttons in table view cells

September 21, 2015
Simulator Screen Shot 20 Sep 2015 21.45.19

UPDATE: There is an upgraded post available where I demonstrate how to use Swift closures to achieve the same result using less (and cleaner) code. Check it out here.

Table views and collections are awesome, right? We’re all pretty much used to customizing cells but every once in a while, we need to go one step further and add a button to the table view cell.

The fastest way to accomplish this is to introduce a protocol in your code. Like this

Now that there’s a protocol in place, we have to define a ButtonCellDelegate variable in our custom table view cell. I’m assuming we already have the button’s action linked to the custom cell:

The delegate in the upper piece of code is in our case the table view controller. Here’s how we assign the delegate in the cellForRowAtIndexPath

Of course, we need to implement the protocol’s method in the table view controller

That’s it! Note that there is some other boilerplate code but you’re free to check the complete project on GitHub or download the zipped project directly.

Happy coding!


Take part in the 5-day course with actionable tasks that will let you become a master at recognizing and solving the most common mistakes iOS developers do with Auto Layout.

Let Auto Layout become a tool you swing with your utmost confidence!

I won't send you spam, I promise. Unsubscribe at any time. Powered by ConvertKit
Hey there! You're already subscribed to my newsletter and you've hopefully gotten some useful tips and tricks when working with iOS. If you're also working with Auto Layout, make sure to check out my book called Auto Layout Fundamentals and get a 20% off for being my subscriber!
Jure Zove

A lot of things but mostly a programmer who really likes fast cars. Check me out on Twitter, if you fancy.

Related article

How to properly do buttons in table view cells using Swift closures

Note: This post is an upgrade of the original post…

A practical MVVM example in Swift – Part 2 (featuring RxSwift)

Welcome to part 2 of the practical MVVM example in…

A practical MVVM example in Swift – Part 1

The good ol’ MVC pattern has been around for a…

  • Manolo Suarez

    Hi Jure, That was great almost what I need :), My idea is a table view with a searchBar, if the user try to find a word and there is no this word in the data, should come a cell with something like sorry there is no such a word in this database, would you like to send to me your search so I can add it in the next update? so there will be a button to send to an email with the user’s search. Do you know where to find something like this? Cheers and thank you for all posts. 🙂

    • Hey Manolo!

      So, the way I’d conceptually approach this task:
      1. in the numberOfRowsInSection …, I’d have an if statement which checks if there are results or not.. if there are no results, return 1
      2. In the cellForRow… I’d do the same – if there are no results, return a special cell which contains the button you want
      3. Use the code above to help you connect the button with an action
      4. In the view controller, get the text from the search bar and continue on with that. 🙂

      Does this help in any way?

  • Excellent…just what I needed to avoid using a tag! 😀

  • Hyesoo Jung

    thank you very much! It’s really helpful to me 🙂

Type your search keyword, and press enter to search