Callbacks in Swift - A practical example with TableView and CollectionView

Jahid Hasan Polash
InfancyIT
Published in
3 min readMar 17, 2018
Photo by jehyun-sung on unsplash

You Love something so much, you always want to be around it.

All programmers become obsessed with something and if you are an iOS dev its obvious you would be one with UITableView and/or UICollectionView as soon as you are introduced to them. You would love them even more when you would be working with UIScrollView hazards (I mean it 😢). I have seen programmers love some data-structures (i.e.enum ,struct ) so much that they can’t think anything but them. This type of love brings some really cool hacks of doing something with the help of something else. So, before starting the Swift Discussion, let me update my quote mentioned before,

You Love UITableView and/or UICollectionView so much that even a simple input FORM seems nothing but these.

See this very simple form for a user input. It has some text fields and some title labels; can easily be designed and maintained in a native UIViewController with UIScrollView(for smaller screens).

But, if you love TableView, you would see this can be implemented pretty easily there too. In my opinion, it has some distinct advantages. But lets see how can this be a problem to implement.

There is no problem with the design and presentation of the form exactly it is with custom cell class, but the Problem is how to transfer data from the custom cell objects to TableViewController object. I will show that by a diagram.

Now, the Return of input text to Parent ViewController can be done in several ways. We can do it with protocol declaration, with Notification userInfo etc. But today I am going to talk about a neat way to do this with callback functions.

Now, callbacks are defined in various ways in various platforms and languages. To simply state, callbacks are returning something back to the caller. It can be returning a value, a reference, a function or returning itself.

Ok ok. I talk too much. Let’s see some code.

Suppose this is a custom UITableViewCell class

which is instantiated by a UITableViewController namedUserController. We can do this inside cellForRowAtIndexPath method of UITableViewDataSource in the controller.

let cell = tableView.dequeueReusableCell(withIdentifier: "singleTfCell", for: indexPath) as! SingleTfTableViewCellcell.staticLbl.text = "Company"
cell.inputTf.text = company ///if the form is in edit user info mode
return cell

We know that. Come on, show the callback already!!

Yes, I am going to. So if we want to save the value of the input textField to a variable named company declared in the UserController we need to return the input value at the point of textFiledDidEndEditing to the UserController from SingleTfTableViewCell object (remember that I am calling it object, not class). This is how I do it with the callback.

And inside the cellForRowAtIndexPath method, I do something like this.

let cell = tableView.dequeueReusableCell(withIdentifier: "singleTfCell", for: indexPath) as! SingleTfTableViewCellcell.staticLbl.text = "Company"
cell.inputTf.text = company ///if the form is in edit user info mode
//return value from callback
cell.returnValue = { value in
self.company = value
}
return cell

Easy, Huh?

So, does it work for multiple cells with same customCellClass ? Yes it does. With great efficiency. We can define all our callback return closures in their separate scopes. Like this:

See how different objects of the same class are assigned to set different variables. ✌

Everything here works for UICollectionView just as much as for UITableView.

So this is all I wanted to share with you today. I know there are lots of more useful examples of CallBacks. I would love to know them all. Please share with me in the responses.

If you like the article give some claps. If you don’t, please don’t hate me. Happy Coding… :)

--

--