I will provide in detail explanation for this.
Here, UITableViewDataSource
& UITableViewDelegate
are actually protocols. Unfortunately, UIKit Framework
is not open source. But I will assure you this is what internally happens after referring many articles.
Protocol is like basketball coach with some requirements in it. He/She tells players like class, struct, enum what to do?
by using those requirements. But He/She doesn't knows
how to do?
by themself. So, the class or struct which conforms that protocol should provide implementation to those requirements while achieving to dunk the ball.
protocol UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
}
A Protocol is said to be DataSource protocol then it always contains required functions with "return type" as shown below.
protocol UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
}
Implementing UITableView inside custom viewController
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
override func viewDidLoad {
tableView.delegate = self
tableView.dataSource = self
}
Here, tableView
acts as Delegator(sender) & viewController object i.e (self)
as Delegate(receiver).
UITableView()
object now referred as tableView
is having two optional stored property of respective protocol as type in UITableView()
class & now it is called by tableView
as
tableView.delegate: UITableViewDelegate?
tableView.dataSource: UITableViewDataSource?
In order to get UITableView
in viewController
.It should conform to both the Protocols.
So, viewController
class object has implemented all those required functions of both the protocols. Now self
can be used either as UITableViewDelegate
type or UITableViewDataSource
type because Protocol can be used as type for an object of class which conforms to it.
Now, both properties of tableView
i.e delegate
& dataSource
are assigned to self
because its having same respective protocol types.
The non-optional functions of both Protocols are implemented in viewController
class object as below
Protocol UITableViewDelegate
functions
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Do further processes like pushing or poping another viewController
}
Protocol UITableViewDataSource
functions
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
}
1) When the user select a row in a section then tableview
(Sender) i.e UItableView()
calls the UITableViewDelegate
func below shown by passing data to parameters tableView
& indexPath
which resides in viewController
object(Receiver) through its delegate
property. Now viewController
uses those passed data to do further processes like pushing or poping to new custom viewController.
tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
2) Functions inside UITableViewDatasource
protocol provides custom data to tableview
(Sender). The tableview
asks the viewController
object by calling Datasource functions with passing data to parameters tableView
& indexPath
which resides in viewController
object(Receiver) through its datasource
property. Now viewController
uses those passed data & returns custom data back tableview
. Now tableview
uses those data to create "10" cells in a section & kind of "cell" at indexpath
tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"
tableView.dataSource?.tableView(UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> returns "cell"
So we assign both tableView.delegate
& tableView.Datasource
with self
. Its not to conform for Protocol. But to initialise properties tableView.delegate
& tableView.Datasource
with self
. So, that tableView
can call those Delegate
& Datasource
methods residing in self
through them.
A class is said to be conforming to protocol by just implementing all its requirements in it. Thats it but not by assigning self
to properties of others.
Finally, whole UIKit Framework
uses delegate & datasource design patterns in all its classes such as UIApplication
, UITableView
, UICollectionView
, UITextField
& so on to communicate data. Unfortunately, UIKit Framework
is not open source.