Menu & Search

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

May 22, 2016

Note: This post is an upgrade of the original post where I used a protocol-oriented approach. In my personal opinion, protocols are great if you know at least 3 classes are going to implement that particual protocol. If it’s only one or two, a simple closure is a much leaner approach.

Let’s assume we have a custom UITableViewCell that’s called ButtonCell, which already contains a label and a button. That button is connected via IBAction to the method called buttonTap(). The class looks like this:

And the UI looks like this:

Screen Shot 2016-05-22 at 17.06.37

To enable ButtonCells to fire off custom blocks of code (closures), we’ll have to add just 4 lines of code. First, we need a variable that will hold the closure.

Next, we’ll need to unwrap the variable and execute it in the buttonTap() IBAction:

And that’s pretty much it! To use this cool new feature, we can simply assign any compatible closure (that has one input parameter of type UITableViewCell) in the part of code where we configure our cell. For example:

A nice GIF to end the tutorial:

buttoned-up-cells-closures

If you’re wondering how the full working project looks like, head over to GitHub or download the zipped project directly!

STOP LOSING TIME WITH AUTO LAYOUT!

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! https://gum.co/autolayoutfundamentals/youareawesome
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

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…

Supporting links in text

If you’ve encountered a use case where you need a…

  • Love this! Very spimple! I’ll likely change mine to this as it’s as simple as this but with an ActivityViewController instead of an AlertController. Thanks.

  • Paweł Wrzosek

    If your closure is optional, you can call it in a bit more consist way: tapAction?(self)

    • Very nice catch, Pawel! Thanks – already corrected. 🙂

  • sarath

    thank you very much @Jure Žove

  • John Armstrong

    That was entirely fantastic. Thank you.

  • Chetan Daksh

    Thnxx a lot #Author You just saved my time even when deadlines are near.!! 🙂

  • Hussein AlBehary

    Thank you very much, I have been looking around for couple of hours with complex code, but you have solved my problem in couple of minutes with 4 lines of code.

    • Thanks for the feedback, I’m happy to hear I was able to help you out. 🙂

  • Evert van Nieuwamerongen

    Wouw, very clever code. Had to do a lot of work to change my entire app, but it worth it. Cleaner is not possible. Got rit now of al my .tag mechanism’s . Thanks @jurezove:disqus

    • Evert van Nieuwamerongen

      And btw, changed var tapAction: ((UITableViewCell) -> Void) to var tapAction: ((MyCustomTableCell) -> Void) to get full access to the label in that custom table cell

      • Yeah, that’s great! Thanks for your feedback! 🙂

  • addictingapps

    does this not cause a reference cycle?

  • Mili Shah

    Thank you so so so much

  • Stephy Samaniego

    thanks so much!

Type your search keyword, and press enter to search