I found a question in a quiz is "Which UIKit protocol contains the method –tableView:heightForRowAtIndexPath:?"
without taking any load the protocol comes in my mind was UITableViewDataSource. But when I checked apple developer documentation, I found I was wrong. UITableViewDelegate is the protocol who contain that method.
Before that I understood was that these height methods are part of Datasource protocol.
Is there any specific reason of placing them in UITableViewDelegate instead of UITableViewDataSource ?
Why following function are placed in UITableViewDelegate
heightForRowAt indexPath
heightForHeaderInSection
heightForFooterInSection
and Why following function are placed in UITableViewDataSource
numberOfSectionsInTableView
numberOfRowsInSection
Before now I thought these are similar type of function so these methods must be placed in same protocol.
Please make this clear to me.
Thanks in advance
UITableViewDelegate means you provide answers to requests about the layout of the table and about actions the user performs on the tableview.
UITableViewDatasource means you provide data for the sections and rows of a table and you act on messages that change a table's data.
For more in details, Check DataSource and Delegate
numberOfSectionsInTableView and numberOfRowsInSection are functions that answer questions about the data to be displayed which is why they are to be found in the DataSource protocol.
The heightFor... functions don’t answer anything about the underlying data but only on the way the data is displayed. This is why they are part of the Delegate protocol.
Does that make sense to you?
The datasource supplies the data, the delegate supplies the behavior.
In MVC, datasource is in the model layer and the delegate is in the control layer.
Actually, on second thought, the datasource is usually controller that is lower down, closer to the model. I don't think I've ever used a model object as my datasource.
The UI prospects of the UITableView are provided by the UITableViewDelgate methods and the data (we are not talking about the data to be used inside the cell, just the information about UI (section, row, etc.) repetitions) needed to construct the UI is provided by UITableViewDataSource methods.
Related
I have a UIViewController in my application that contains a UITableView. This tableView has a few different states for section 2. The rows in this section can vary by height, cell type and number of cells.
The way I used to handle this was one UIViewController with lots of different if-statements in the UITableViewDelegate and UITableViewDataSource. Now, after a while, this has given me quite a lengthy and complicated controller.
I thought about two possible routes to fix this. The first one would be different UITableViewDelegate and UITableViewDataSource classes, based on an if-statement. The other would be to load in a different UITableViewController for each of the possible states.
What do you guys think would be the cleanest solution? Or are there any other cleaner solutions?
Firstly create an extension for viewcontroller which confirms to tableView dataSource and delegate protocols.
To achieve this we can create a custom method in presenter class to handle all this code and call this method whenever required.
I'm following a UITableView tutorial and I've learned that the UITableView is a view object which doesn't handle the logic or data, it primarily the view or user interface. As I continued reading it says that the UITableView requires a "datasource". My question is this, is the datasource the program's logic?
IOS app development widely use Delegate design pattern. Almost all UIView have their own Delegate Protocol. Before understand UITableViewDelegate and UITableViewDatasource Protocol try to learn about Delegate design pattern.
If you work with UITableView you have to implement atleast 2 protocol in your ViewController.
1. UITableViewDelegate : The delegate of a UITableView object must adopt the UITableViewDelegate protocol. Optional methods of the protocol allow the delegate to manage selections, configure section headings and footers, help to delete and reorder cells, and perform other actions.
2. UITableViewDataSource: The UITableViewDataSource protocol is adopted by an object that mediates the application’s data model for a UITableView object. The data source provides the table-view object with the information it needs to construct and modify a table view.
Why is tableView:canMoveRowAtIndexPath: is UITableViewDataSource protocol and not in UITableViewDelegate protocol?
Similar methods (e.g. tableView:canFocusRowAtIndexPath:) are in Delegate protocol. I don't think it's a mistake, so can anyone explain why such method is part of the data source and not the delegate?
A general explanation of which methods belong to data source protocols and which belong to delegate protocols is also appreciated.
The delegate methods generally have to do with the appearance of the table view.
The data source methods generally have to do with the content of the table view. It's often the case that the displayed content's order is fixed. Say the table view was displaying stops on a bus line, or the chapter headings of a book. You can't let the user reorder those: it isn't something that the content itself supports.
Notice that both delegate and data source are actually involved in the decision as to whether a row can move. The data source gets the method you named, but the delegate gets asked tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: at the same time.
The UITableViewDatasource protocol documentation:
The UITableViewDataSource protocol is adopted by an object that
mediates the application’s data model for a UITableView object. The
data source provides the table-view object with the information it
needs to construct and modify a table view.
As a representative of the data model, the data source supplies
minimal information about the table view’s appearance. The table-view
object’s delegate—an object adopting the UITableViewDelegate
protocol—provides that information.
The required methods of the protocol provide the cells to be displayed
by the table-view as well as inform the UITableView object about the
number of sections and the number of rows in each section. The data
source may implement optional methods to configure various aspects of
the table view and to insert, delete, and reorder rows.
Hope, that clears things out.
EDIT: With my own words (but repeating the docs): Datasource declares methods those somehow directly or indirectly affect/reflect the data model, whereas the method tableView:canFocusRowAtIndexPath: can't be said similar to tableView:canMoveRowAtIndexPath: because it has nothing to do with the data. That said, datasource carries constructive character, delegate - informative.
I'm wondering if it is possible to handle custom section headers when using static table cells. It doesn't seem possible through Interface Builder... the datasource is not called, so the method viewForHeaderInSection is never called too.
I'm not interested in workarounds like using a cell as section header.
Method viewForHeaderInSection is part of UITableViewDelegate protocol so you can adopt it and use that method for your section header.
Can you please check whether you have made a connection between the interface builder and your ViewController.
delegate and datasource should be connected.
I understood before that
Delegate pattern is used only for invoking events to delegate instance and getting controls (like size / font / etc...).
Datasource pattern is only for getting data from datasource instance (like views / title / description / etc...)
But seems it was a nice illusion, after looking to Apple's UITableViewDelegate protocol I got confused because
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
Are delegate methods (but I was thinking that they are UITableViewDatasource methods)
Is this a dirty code from Apple, or I'm missing something important too understand difference between datasource and delegate?
EDIT:
Thanks #DBD for nice answer,
here is more confusion
Here is UITableViewDelegate method that returns View for drawing
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
And also there is a configuration in UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
And oops, we can see a method that returns a View in UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Here we have question why cellForRowAtIndexPath: and viewForHeaderInSection: are not in UITableViewDataSource
This is how I've always thought about it.
UITableViewDataSource to be primary data. What are the actual contents of the table. How many rows? What is the content of row X?
UITableViewDelegate was secondary and display data. How tall should it be, should it display in the selected state, and call backs for "hey I'm about to do something."
However I admit I see some of it as a fine line (and I don't buy some of the choices)
UITableViewDataSource has titleForHeaderInSection.
UITableViewDelegate has viewForHeaderInSection.
So if it's pure "data" title, it's the data source, but if includes a display wrapper with a view, it's the delegate. But wait, cellForRowAtIndexPath is a view and that's part of the data source, so why would you put viewForHeaderInSection in the delegate? While I can barely see the distinction between as "cell" as data and "title view" as delegate, I think the confusion of splitting "title" methods into different protocols is not preferable. I'm sure many might disagree with me, but it's just my opinion.
I think the critical distinction here arises from what you consider "data." From your question, I think you understand "data" to mean "any return value" – that is, methods which return void are delegate methods, and methods which return non-void are data source methods (since they pass something back to the sending table view).
This can sometimes be a useful approximation, but here is inaccurate. A table view's data is the contents that it displays – the stuff in the cells, the titles of sections, etc. Any other information, including that about layout (like row height) or display (like section headers) properly belongs in the delegate, since it is not about the contents of the table – merely about how to display those contents.
The two are very often related, which is why more often than not the same UITableViewController subclass implements both the delegate and data source, but imagine: you could have one object act as the data source and vend cells, then have a different object act as the delegate and provide heights for your rows based on completely different criteria. (Imagine a table where the user can resize rows, for example. You still provide the contents of each row, but the height – the delegate's responsibility – is drawing from a very different set of information.)
dataSource and delegate are both protocols but they are separated into two terms so that we can better understand what the methods are designed to do.
This means:
The dataSource protocol defines an API that supplies the data where delegate supplies the behavior.
dataSource is in the model layer and the delegate is in the control layer.
I think this is the correct outlook.
I have the same confusion with you, until I see the Apple's document.
The UITableViewDataSource protocol is adopted by an object that
mediates the application’s data model for a UITableView object. The
data source provides the table-view object with the information it
needs to construct and modify a table view.
As a representative of the data model, the data source supplies
minimal information about the table view’s appearance. The table-view
object’s delegate—an object adopting the UITableViewDelegate
protocol—provides that information.
UITableViewDataSource Protocol Reference
I don't understand your point.
The datasource protocol methods are all related to the data. The delegate protocol instead has methods regarding the appearance of the cells.