UITableViewDelegate and UITableViewDatasource confusion
我之前就明白了
但看起来这是一个很好的错觉,在看了Apple的
1 2 3 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; |
委托方法(但我认为它们是
这是来自Apple的脏代码,还是我遗漏了一些重要的东西,了解数据源和委托之间的区别?
编辑:
谢谢@DBD的好评,
这里更混乱
这是UITableViewDelegate方法,它返回View以进行绘制
1 | - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; |
而且UITableViewDataSource中还有一个配置
1 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; |
而且,我们可以看到一个在UITableViewDataSource中返回View的方法
1 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; |
这里我们有问题为什么
这就是我一直在想的。
但是我承认我看到它的一些细节(我不买一些选择)
因此,如果它是纯粹的"数据"标题,那么它就是数据源,但如果包含带有视图的显示包装器,那么它就是委托。但等等,
我认为这里的关键区别源于您认为的"数据"。从你的问题来看,我认为你理解"数据"是指"任何返回值" - 也就是说,返回
这有时可能是一个有用的近似值,但这里不准确。表格视图的数据是它显示的内容 - 单元格中的内容,部分的标题等。任何其他信息,包括有关布局(如行高)或显示(如部分标题)的信息都正确属于委托,因为它不是关于表格的内容 - 仅仅是关于如何显示这些内容。
这两者经常是相关的,这就是为什么通常不同的UITableViewController子类同时实现委托和数据源,但想象一下:你可以让一个对象充当数据源并销售单元格,然后让另一个对象充当代表并根据完全不同的标准为您的行提供高度。 (例如,用户可以调整行的大小,例如。您仍然提供每行的内容,但高度 - 代表的职责 - 是从一组非常不同的信息中提取的。)
这意味着:
我认为这是正确的前景。
我和你有同样的困惑,直到我看到Apple的文件。
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协议参考
我不明白你的观点。
数据源协议方法都与数据相关。代表协议具有关于单元格外观的方法。