Buttons in UITableView (Protocols and Delegates)

iDev
4 min readOct 7, 2020

This last project I did really helped me understand the use of Protocols and Delegates. I had somewhat understood the concept but it was really solidified when I had to implement and create my own protocols and delegates for a certain favorite button.

Protocols

Let’s try to understand this concept of protocols. Just as you might use and understand protocols in any other daily use, it’s used the same way in Swift. This is one reason why Swift is so great in my opinion because Apple chooses names that perfectly describes what exactly the code is doing. The names aren't ambiguous.

Just as we might say today, “stick to the protocol”, this implies there is a certain way things must be done. This is exactly what happens when a class or struct in our code adopts a protocol. The class is taking on the rules of the protocol and promising to adhere to them. With these protocols, there may or may not come certain functions that must be implemented.

I heard it explained this way and it made sense to me. Let’s say there is an Emergency Call Handler who receives calls and then depending on the call, the Call Handler sends a message to a pager. The Call Handler doesn’t care who has the pager, because in order be able to carry the pager, they must be CPR certified.

This CPR certification would be our Protocol. Anyone(class or struct) who is CPR certified(adopts this protocol) can wear the pager and respond to the message. And with this certification, you have the ability(function) to perform CPR. So when the Call Handler sends the message that someone needs CPR, they know whoever has the pager has the ability to perform CPR because they are certified.

Delegates

So what happens when there are multiple people(classes or structs) that are CPR certified(protocol)? This where delegates come in. Just as you might be a supervisor at a job and “delegate” certain responsibilities to different people, the same is for setting the delegate of the protocol. In this example, the pager is the delegate.

Remember pagers? Haha
Remember pagers? lol

Let’s say there are three people all working at the same time in a hospital, a doctor, a nurse, and a paramedic. They all have the certification and can perform CPR, but only one is going to take on the responsibility and wear the pager. This is a class or struct assigning the delegate to themself. Taking the responsibility of receiving the message. So when the Call Handler sends the message, it’s only the person wearing the pager that responds, not everyone.

Buttons in UITableViews

So how do protocols help with buttons in UITableViews? Think of building an app like the layers in a cake or stories in a building. Without making this too complicated let’s imagine a blank screen on a phone is the foundation. A blank concrete slab. We then add a UITableView to the screen. This is our bottom level or first floor of the building. Then on top of the UITableView, we put a cell. This cell holds an image, words, and a favorite button. This is the second floor of our building.

Now the first floor has no idea what’s going on on the second floor. The UITableView has no idea that a button inside of a cell has been tapped. You have to tell the UITableView(first floor) that something happened(the button was tapped) on the second floor(cell).

So we need to create a protocol that UITableView adopts. Because we are gonna send a message from the cell(first floor) to anyone who has this certain protocol. But that’s not enough, what if ever floor on that building also adopts that protocol too. Which floor do we send the message to? Well, whoever has the delegate. And this is where we assign the delegate to the first floor, our UITableView! Now whenever the button is tapped in the cell, it sends data to the UITableView and we perform a certain function or whatever we want.

I hope this helped you understand protocols and delegates and my dilemma this week. This is how I solved my issue by implementing protocols.

Rick Martinez

--

--