What is a table view delegate?


At one point the author makes us write this code below, now what is the table view delegate mean here , in the earlier code the table view was asking for how many rows to display etc and we were providing it the info, though I do not still fully understand how that was happening , but here what is the tableview trying to do,

is it asking a question , then why call it a delegate, is it saying then when a row is tapped do this? If so to whom the Information is getting passed, to the tableView itself ?

Why we call it a delegate ? Why we use tableView: UITableView, again and again , kindly guide , thanks

// MARK:- Table View Delegate
    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)

Hi @amitsrivastava, the code above manages the selection from the tableview. It tells the delegate that a row has been selected. The delegate is the UITableViewDelegate is already being adopted in your file because I believe your class is using UITableViewController. If you were using a UIViewController and then added a tableview inside, then you would have to add UITableViewDelegate to the top of the class. (Ex: class ChecklistViewController: UIViewController, UITableViewDelegate, etc).


1 Like

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