UITableViewDelegate and UITableViewDatasource confused

I understood before that the Delegate template is used only to call events in a delegate instance and get controls (for example, size / font / etc ...).

Datasource template is intended only for receiving data from a datasource instance (for example, views / title / description / etc ...)

But it looks like it was a good illusion, looking at the Apple 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 thought these were UITableViewDatasource methods)

Is this dirty Apple code, or am I missing something important that also understands the difference between a data source and a delegate?

EDIT: Thanks @DBD for the nice answer, there is more confusion here

Here is a 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 unfortunately, we can see a method that returns a view in a UITableViewDataSource

  - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath;

This begs the question why cellForRowAtIndexPath: and viewForHeaderInSection: not in UITableViewDataSource

+7
design ios objective-c uikit uitableview
source share
5 answers

This is how I always thought about it.

UITableViewDataSource - primary data. What is the actual contents of the table. How many lines? What is the contents of line X?

UITableViewDelegate was secondary and display data. How tall it should be, whether it should appear in the selected state, and the callback for "hey, I'm going to do something."

However, I admit that some of them are thin lines (and I do not buy some options)

UITableViewDataSource has titleForHeaderInSection . UITableViewDelegate has viewForHeaderInSection .

So, if this is a clean "data" header, this is a data source, but if it includes a display shell with a view, then this is a delegate. But wait, cellForRowAtIndexPath is the view and this part of the data source, so why would you put viewForHeaderInSection in the delegate? Although I can barely see the difference between a “cell” as data and a “header” as a delegate, I think that confusing the separation of the “headers” of methods into different protocols is not preferable. I am sure that many may disagree with me, but this is just my opinion.

+8
source share

I think the critical difference here arises from what you consider "data." From your question, I think you understand that “data” means “any return value,” that is, methods that return void are delegate methods, and methods that return non void are methods of the data source (because they pass something then back to the submission table view).

This can sometimes be a useful approximation, but here is inaccurate. Table view data is the content that it displays — material in cells, section headings, etc. Any other information, including the layout (for example, row height) or the display (for example, section headings), belongs properly to the delegate, since this is not about the contents of the table — just how to display this content.

These two are very often connected with each other, so most often the same subclass of UITableViewController implements both a delegate and a data source, but imagine: you can use one object as a data source and provider cells, and then have different objects act as delegate and provides height for your lines based on completely different criteria. (Imagine a table where the user can resize the rows, for example. You still provide the contents of each row, but the height — the delegate’s responsibility — is drawn from a completely different set of information.)

+2
source share

dataSource and delegate are both protocols, but they are divided into two terms, so that we can better understand which methods are designed for this.

It means:

  • The dataSource protocol defines an API that supplies data, where delegate provides behavior.
  • dataSource is at the model level, and the delegate is at the management level.

I think this is the correct forecast.

+1
source share

I have the same confusion with you until I see the Apple document.

The UITableViewDataSource protocol is adopted by an object that mediates the application data model for the UITableView object. the data source provides an object of the form of a table with the information that it needs to create and change the presentation of the table.

As a representative of the data model, the data source has minimal information about the appearance of the tables. . The table view of the object delegate — an object that accepts the UITableViewDelegate protocol — provides this information.

Link to UITableViewDataSource Protocol

0
source share

I do not understand your thought.

Data source protocol methods are data related. Instead of the delegate protocol, there are methods regarding the appearance of cells.

-one
source share

All Articles