UITableView中的数据源和委托如何工作? [英] How does data source and delegate in UITableView works?

查看:47
本文介绍了UITableView中的数据源和委托如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios开发的新手.当我使用UITableView时,我实现数据源并一起委托.类似于以下两种方法:

I am new in ios development. When I use UITableView, I implement data source and delegate together. like the following two methods:

 // Data source method
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 // Delegate method
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

但是,如果我理解正确,表视图将不保存任何数据,并且仅存储足够的数据以绘制当前可见的行.因此,例如,如果我的表中有10个数据,而当前仅显示5个.这意味着委托方法运行5次,但是在委托方法运行5次之前,数据源方法已经运行10次以获取行数.我们之所以使用数据源,是为了管理使用集合视图呈现的内容.所以我的问题是,数据源如何管理内容?数据源对象是否存储所有这些表视图信息?(因为它在委托之前运行,并且知道表视图的总数)如果它存储表视图的信息,则似乎与委托冲突,因为表视图委托不保存任何数据, 正确的?

However, if I am understand correctly, table view does not hold any data, and it only store enough data to draw the rows that are currently visible. Therefore, for instance, if I have 10 data in table, and only shows 5 currently. That means delegate method runs 5 times, but before delegate method runs 5 times, data source method already runs 10 times to get the number of rows. And the reason we use data source is to manage the content that presents using a collection view. So my question is, how does data source manage the content? Does data source object store all of those table view information?(since it runs before delegate and it knows the total numbers of table view) If it stores information of table view, it seems conflict with delegate since table view delegate does not hold any data, right?

另一个问题是,在什么情况下我们仅使用数据源?因为我们可以创建自定义委托吗?是否只有我们创建自定义数据源的情况?由于我已经看到数据源总是随委托提供的....谢谢!

One more question is in what situation we just use data source? because we can create custom delegate right? Is there situation we only create custom data source? Since I've seen data source always comes with delegate.... Thanks!!!

推荐答案

UITableViewDataSource 协议定义了 UITableView 用数据填充自身所需的方法.它定义了几种可选方法,但有两种是必需的(非可选):

The UITableViewDataSource protocol defines the methods the UITableView needs to populate itself with data. It defines several optional methods but there are two that are required (not optional):

// this method is the one that creates and configures the cell that will be 
// displayed at the specified indexPath
– tableView:cellForRowAtIndexPath: 

// this method tells the UITableView how many rows are present in the specified section
– tableView:numberOfRowsInSection:

此外,以下方法不是必需的,但也是实现的好主意(也是部分数据源)

Also, the following method is not required but is also a good idea to implement (part of the data source too)

// this method tells the UITableView how many sections the table view will have. It's a good idea to implement this method even if you just return 1
– numberOfSectionsInTableView:

现在,对于 UITableView 中的每个 visible 单元格,方法 –tableView:cellForRowAtIndexPath:将运行一次.例如,如果您的数据数组有10个元素但只有5个可见元素,则 –tableView:cellForRowAtIndexPath:将运行5次.当用户向上或向下滚动时,将为每个可见的单元格再次调用此方法.

Now, the method –tableView:cellForRowAtIndexPath: will run once for every visible cell in your UITableView. For example, if your data array has 10 elements but only 5 visible, –tableView:cellForRowAtIndexPath: will run 5 times. As the user scrolls upwards or downwards this method will be called again for each cell that becomes visible.

您所说的:数据源方法已经运行10次以获取行数."是不正确的.数据源方法 –tableView:numberOfRowsInSection:不能运行10次以获取行数.实际上,此方法仅运行一次.另外,此方法在 –tableView:cellForRowAtIndexPath:之前运行,因为表视图需要知道必须显示多少行.

What you said: "(the) data source method already runs 10 times to get the number of rows." is not true. The data source method –tableView:numberOfRowsInSection: does not run 10 times to get the number of rows. In fact this method only runs once. Also, this method runs before –tableView:cellForRowAtIndexPath: because the table view needs to know how many rows it has to display.

最后,方法 –numberOfSectionsInTableView:也运行一次,并且在 –tableView:numberOfRowsInSection:之前运行,因为表视图需要知道可能会有多少节.请注意,此方法不是必需的.如果您未实现,则表格视图将假定只有一个部分.

Finally, the method –numberOfSectionsInTableView: runs also once and it runs before –tableView:numberOfRowsInSection: because the table view needs to know how may sections will there be. Please note this method is not required. If you don't implement it the table view will assume there will only be one section.

现在,我们可以将注意力集中在 UITableViewDelegate 协议上.该协议定义了与 UITableView 的实际交互作用有关的方法.例如,它定义了以下方法来管理单元格选择(例如,当用户点击某个单元格时),单元格编辑(插入,删除,编辑等),配置页眉和页脚(每个部分可以有一个页眉和页脚),等等.在 UITableViewDelegate 中定义的所有方法都是可选的.实际上,您完全不需要实现 UITableViewDelegate 即可获得表视图的正确基本行为,即显示单元格.

Now we can focus our attention on the UITableViewDelegate protocol. This protocol defines methods that have to do with the actual interaction with the UITableView. For example, it defines methods to manage cell selection (when the user taps a cell, for example), cell editing (inserting, deleting, editing, etc), configure headers and footers (each section can have a header and a footer), etc. All the methods defined in UITableViewDelegate are optional. Actually you don't need to implement UITableViewDelegate at all to get the correct basic behavior of the table view, which is, to display cells.

UIcodeViewDelegate 的一些最常见方法是:

Some of the most common methods of UITableViewDelegate are:

// If you want to modify the height of your cells, this is the method to implement
– tableView:heightForRowAtIndexPath:

// In this method you specify what to do when a cell is selected (tapped)
– tableView:didSelectRowAtIndexPath:

// In this method you create and configure the view that will be used as header for
// a particular section
– tableView:viewForHeaderInSection:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆