Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self.tableView.delegate = self Swift

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 { ... }

}
like image 713
Lance Samaria Avatar asked Sep 12 '16 01:09

Lance Samaria


People also ask

What does tableView delegate self mean?

Here, tableView acts as Delegator(sender) & viewController object i.e (self) as Delegate(receiver). In order to get UITableView in viewController . It should to conform to both the Protocols. So, viewController class object has implemented all those required functions of both the protocols.

What does tableView delegate do?

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

What is the difference between delegate and datasource?

A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data. A data source is an outlet held by NSView and UIView objects such as table views and outline views that require a source from which to populate their rows of visible data.

What is a delegate in Swift?

In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object. One example is the UIApplicaitonDelegate in an iOS app.


2 Answers

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.

like image 56
matt Avatar answered Sep 19 '22 12:09

matt


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 /.

like image 26
Quang Hà Avatar answered Sep 22 '22 12:09

Quang Hà