Suppose you had 3 tableviews. For dogs, cats and birds. Tapping on each cell would show a new screen with the enlarged photo of it.
To design this, you'll need to come up with 3 separate datasources for dogs, cats and birds. You basically need three arrays.
However you don't need 3 tableview delegates. Because the behavior of the tableviews are all the same. They all just take present a viewController and fill it with a UIImage. This is only true if you delegate is written in a generic way i.e. there's no dog, cat or bird specific code in the delegate.
Having that said you could abstract out the dog, cat, bird from the data source, but my answer was just a contrived example. Some custom objects are too complex to use the same structure, hence the need to have 3 datasources.
Old answer:
Before answering the question, you must better understand the delegation design pattern:
Let me begin with a question:
By default a TableView is like this:
How does a UITableView know how many cells to present? what to present in each cell?
- By itself, it doesn't know.
- It asks another class to inform it about the number of cells and what cell to return ( what cellimage, celltitle, cellsubtitle,etc.) values to itself. You usually see a tableView (delegating class) inside a ViewController (delegate class)
- This concept of one class asking another is known as delegation!
Now that you know what Delegation is, to answer the actual question of the OP:
It's mostly a HUGE matter of semantic differences.
If you are only to use ( not to create your own protocol) foundation's delegates and datasources then it really doesn't matter for you. However if you intend to write custom protocols then understanding them would help you to write ( and with a higher importance read, refractor) code better.
From a developer's point of view, They both deal with the interaction between the delegat-ing class and delegate class.
Data Source
A data source is almost identical to a delegate. The difference is in
the relationship with the delegating object. Instead of being
delegated control of the user interface, a data source is delegated
control of data. The delegating object, typically a view object such
as a table view, holds a reference to its data source and occasionally
asks it for the data it should display. A data source, like a
delegate, must adopt a protocol and implement at minimum the required
methods of that protocol. Data sources are responsible for managing
the memory of the model objects they give to the delegating view.
In Layman's terms:
DataSource deals mostly with what and usually does it's stuff upon initialization.
Delegate deals mostly with how and feeds you some parameters to give a certain behavior ie if the user clicked this... what should happen? if they swiped...what should happen?
As an example for tableView:
DataSource
What does it have inside of it? What kind of cell am I presenting? cellForRowAtIndexPath
.
What is the title of Section? titleForHeaderInSection
How many cells are they? numberOfRowsInSection
And therefore you usually return values. For delegates it's more common to be of type void
.
Datasource methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell // return a cell ie UITableViewCell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int // return a number ie an Int
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // return the title ie a String
Delegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath)
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath)
I obviously chose selectively as some datasource methods don't return and some delegate method do return
Delegate
What should I do/what 'form of behavior' should I use after finishing the display of the footer, do you want me to pop an alert?didEndDisplayingFooterView
Am I going to have accessoryType that gives the cell some extra features? accessoryTypeForRowWithIndexPath