fs_tigre
fs_tigre

Reputation: 10738

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?

Upvotes: 7

Views: 10126

Answers (2)

Bruno Campos
Bruno Campos

Reputation: 403

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.

Upvotes: 5

fs_tigre
fs_tigre

Reputation: 10738

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

Upvotes: 12

Related Questions