I will summarize the basics of Delegate, assuming that a friend who is a non-engineer and is studying Swift recently asks "What is Delegate?".
The Japanese translation of the delegate is "delegation". The meaning of delegation is to hand over or leave it to us.
Delegate in swift will also be a delegation feature!
In a nutshell, there are many explanations that say, "One class can let an instance of another class do the work."
Who = "Class A" Will be.
In other words, a class takes care of itself.
In the case of tableview, the "UITableView class" leaves its own processing to others.
What = "In class B" Will be.
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
When creating a tableView with swift, I think that it is common to use this notation.
In this example, "delegate" is left to "self"! And here, self means "view controller" that describes this code. In other words, it's up to the "view controller" that has this code!
What = "Processing yourself (class A)" Will be.
And this process is specifically called "delegate method". Therefore, officially, "processing in its own delegate method" is delegated (delegated) to another class instance.
If it is a table view
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
Etc. correspond to the delegate method.
-Delegate has a function that "a certain class A can entrust processing (a delegate method of class A) to an instance of class B."
Recommended Posts