1

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Behdad
  • 941
  • 12
  • 23

2 Answers2

2

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • As you said, data source methods have to do with _content_, but `tableView:canMoveRowAtIndexPath:` doesn't have anything to do with content. It's all about the appearance of the table view. I'm not convinced yet. (I agree that moving rows at a table view is a matter of both content and appearance, but I still believe `tableView:canMoveRowAtIndexPath:` is related to appearance, not content.) – Behdad Mar 12 '16 at 08:46
  • 1
    @Behdad well, the datasource has to be able to handle moves of the data so I think it is fair to ask it if it can do that :) – Daij-Djan Mar 12 '16 at 08:52
  • @JoshCaswell It sounds acceptable. `tableView:canMoveRowAtIndexPath:` has a little to do with data objects (deciding if the data is move-able at all), so I think that's the reason they included it in the DataSource protocol. I upvoted your answer, but I'll wait for other answers before accepting it. Thanks. – Behdad Mar 12 '16 at 09:01
  • Keep in mind the distinction between the _row_ and the _cell_, @Behdad. The cell is a view; the row is really part of the model layer. – jscs Mar 12 '16 at 09:04
  • @JoshCaswell Well, that didn't help! Then one may ask why `tableView:cellForRowAtIndexPath:` is in the DataSource and not the Delegate. – Behdad Mar 12 '16 at 09:07
  • Yeah, @Behdad, I can definitely see your point, but the data source is the thing that has access to the content that needs to be put into the cell. Let me try to reformulate or expand that. – jscs Mar 12 '16 at 09:14
-1

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.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • Any reason for downvoting? – Fahri Azimov Mar 12 '16 at 08:36
  • -1: normally, it is never a good idea to JUST quote the docs.. say at least a sentence with your own words + in this: the op is even linking to the docs ;) – Daij-Djan Mar 12 '16 at 08:53
  • @Daij-Djan, I was going to say all this with my own words, but discovered that it's already said better in the docs. That's why just posted the piece from docs to pinpoint the main idea. – Fahri Azimov Mar 12 '16 at 08:59
  • 1
    @FahriAzimov I didn't downvote, but I think it's clear why. You didn't answer my question. It's clear from my question that I know what DataSource and Delegate protocols are for, but your answer only explains what a DataSource protocol is. It doesn't answer my question at all. – Behdad Mar 12 '16 at 09:04
  • 1
    @FahriAzimov see my + sentence in addition to what he said – Daij-Djan Mar 12 '16 at 09:13