6

If I have a UIViewController and I hook up a tableView to it in storyboards, connect the tableview outlet, and then connect the datasource and delegate methods via the connections inspector (cntrl+drag to vc orange circle icon), do I still need to add self.tableView.delegate = self and self.tableView.datasource = self to the actual view controller? Of course in the actual vc I'm implementing the tableView data/delegate protocols.

Also I'm assuming whatever the answer is the same would go for a collection view controller being connected via storyboard the same way?

What are the pros and cons of adding it?

class FooController:UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.datasource = self
}


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

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

}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • Based on you tagging the question with protocols it seems that are slightly confused about protocol vs. delegate. Most iOS developers get confused about it initially. I recommend you see this question [here](http://stackoverflow.com/a/39457213/5175709) – mfaani Sep 13 '16 at 12:55

3 Answers3

12

do I still need to add self.tableView.delegate = self to the actual view controller?

No. You are doing this by making the connection in the storyboard. They are exactly the same thing: an assignment to the delegate property, expressed in two different ways (one in code, one "graphically" in Interface Builder). You only need to do one of them.

But if your view controller were a table view controller (UITableViewController subclass), then you would do neither, because the table view controller is already the table view's data source and delegate. The same is true for a collection view controller (UICollectionViewController subclass); it is already the collection view's data source and delegate.

matt
  • 515,959
  • 87
  • 875
  • 1,141
3

I usually set delegate and dataSource programmatically in View Controller's viewDidLoad method. Because sometimes I missed to set them in nib or I created my table view by coding.

As Matt's answer, they're the same. You can pick one way to make you happy. Happy coding, bro /.

Quang Hà
  • 4,613
  • 3
  • 24
  • 40
0

I am agreed with both Matt and Quang answer but sometimes we need to reuse same tableview and its delegate & data source in other ViewController class at that time connection with outlet for datasource and delegate might not be work thatmeans your delegates methods might not be called .

Example -

I have a ViewController(class A) with a tableview.I need to reuse this controler whole view to another class (Class B)

A.view.addSubview(B.view)

So in this case you must need to write below codes

Class A:UIViewController, UITableViewDataSource, UITableViewDelegate{


@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
      super.viewDidLoad()
      self.tableView.delegate = self
      self.tableView.datasource = self 
   }


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
{


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  }
Bhagabata
  • 463
  • 4
  • 12