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 { ... }
}
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.
Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.
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.
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.
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.
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 /.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With