I am learning swift 2.0, and I was wondering if you still need to add the code tableView.datasource = self
and tableView.delegate = self
like in Ob
First of all, that is completely independent of which language you use, Swift or Objective-C.
But there are two different cases which may cause the confusion:
A UITableViewController
subclass:
UITableViewController
already conforms to UITableViewDataSource
and UITableViewDelegate
. It has a tableView
property,
whose dataSource
and delegate
property are already set to self
.
In your subclass, you typically override the data source and delegate methods.
A UIViewController
subclass with a UITableView
property:
Here you have defined a UITableView
property in your subclass
and initialize it in your code, or
connect it to a table view in the interface builder.
In this case you have to set the dataSource
and delegate
property of the tableview, either in code or in the interface
builder, as explained in luk2302's answer.
If data source and delegate are the view controller itself, then you have to declare the protocol conformance explicitly, and implement the data source and delegate methods (but without overriding a superclass method).
Of course, in both cases, the table view data source and the delegate can be set to a different object, it does not have to be the view controller itself.
Yes, some assignment is still required.
Either explicitly via code
OR
What you can do instead is connect them already in the interface builder, making the explicit assignment via code obsolete. That is probably what a lot of tutorials do.