What exactly is a delegate?

Hi,

I have read the chapter again and again still I am not able to understand …

So I am inside – class ChecklistViewController: UITableViewController {

Then there are methods -

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            if cell.accessoryType == .none {
                cell.accessoryType = .checkmark
            }
            else {
                cell.accessoryType = .none
            }
        }
        tableView.deselectRow(at: indexPath, animated: true)
    }

But what is delegate , what is happening here

What is this tableView method again and again ,is it the delegate, who it is sending back data to , to the designView ?

Kindly guide ?

Amit

@amitsrivastava Please let us know what you don’t understand exactly here: what this particular method does or what is a delegate in general. Thank you!

@shogunkaramazov - I do not understand any thing , I do not understand from the very start where they do this - — class ChecklistViewController: UITableViewController

so now it starts having all these. override func tableView methods so are. they in the UITableViewController ?

Why are they being repeated

What are they ? are they delegates? If so what is delegate, I do not know ?

Thanks

The class ChecklistViewController inherits the UITableViewController class meaning it has access to all the properties and functions of the UITableViewController class. Nothing to do with delegates. Delegate is a design pattern used by apple throughout. It’s really more of a way to get control of an action when it occurs. Before the table view cells are displayed, the table view ask how many rows do I need to make. Thats where the function numberOfRows gets called. It asks how many sections, and that’s where numberOfSections gets called and so on. The TableViewController class has no idea how to answer those questions so you have to override the function. There are a lot of different functions that are called when setting up and displaying a table view that you might want to make custom to your app. So instead of using the base functionality, you can create your own.

2 Likes

A delegate class is a class that is responsible for something. i.e. it has been delegate’d responsibility.

In the example given, the class ChecklistViewController has been declared to be of a UITableViewController class type. Whilst it is not a delegate, per-se, the UITableViewController class type is much of a likeness to a UITableView delegate. i.e. it is responsible for a UITableView.

A standard UIViewController may also be delegate’d such responsibilities. For example, if ChecklistViewController were actually just a UIViewController and you wanted it to be responsible for a UITableView object inside the view, you would mark it as being a UITableViewDelegate in addition to a UIViewController (See example below).

class ChecklistViewController: UIViewController, UITableViewDelegate {...}

Regardless of which way you do this (i.e. use a UITableViewController type or a UIViewController type with UITableViewDelegate) the resulting class is responsible for handling events of the UITableView and to do that it needs special methods.

2 Likes

This topic was automatically closed after 166 days. New replies are no longer allowed.