Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect dataSource and delegate with code - iOS Swift

I'm trying to have a better understanding on how the dataSource and delegate outlets get connected to the UITableView under the hood when you do the connection through the UI in Xcode by dragging and dropping to the viewController icon.

I found this thread but I think I'm missing something because I cannot make it work.

Here is the code I currently have that works fine by connecting the outlets through XCode (by drag and dropping).

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var hobbies:[String] = ["Computers", "Photography", "Cars", "Reading", "Learning New Things"]


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return hobbies.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text =  hobbies[indexPath.row]

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

I tried removing the outlet connections made by XCode, created an outlet for the tableView (myTable) and added the following code in the viewDidLoad method but it doesn't work, no error it just doesn't load the data.

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

           myTable.delegate = self
           myTable.dataSource = self
        }

Can someone describe the steps needed to do this connection with code?

like image 577
fs_tigre Avatar asked Nov 13 '15 13:11

fs_tigre


People also ask

What is delegate and datasource method in Swift?

Datasource methods are used to generate tableView cells,header and footer before they are displaying.. Delegate methods provide information about these cells, header and footer along with other user action handlers like cell selection and edit..

What is tableView delegate?

Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.

What is delegate in iOS with example?

What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.

What is Uitableviewdatasource?

The methods that an object adopts to manage data and provide cells for a table view.


2 Answers

Just for reference here are the steps needed to do your connection programmatically.

1.- Create outlet for tableView

 @IBOutlet weak var myTable: UITableView!

2.- Assign delegate and dataSource in the viewDidLoad method.

myTable.delegate = self
myTable.dataSource = self

3.- DONE

like image 108
fs_tigre Avatar answered Sep 24 '22 02:09

fs_tigre


There are a couple of ways to do it.

1. The simplest one is by dragging and dropping:

Dragging and dropping

  • In your main.storyboard select your TableView;
  • Press your Control button;
  • Click and drag the mouse from your TableView to your ViewController's icon and drop it;
  • Then select dataSource and delegate as shown on the image above.

2. The other way is by coding:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

PS: Make sure not to forget connecting your tableView outlet to your code by dragging and dropping it into your ViewController class.

PPS: It'll also ask you to implement the following methods into your class so that your tableView works properly:

  • numberOfRowsInSection
  • cellForRowAtIndexPath

For those who don't have them yet, you'll see Xcode complaining about it.

like image 33
Bruno Campos Avatar answered Sep 26 '22 02:09

Bruno Campos