ios - UITableView 中的数据源和委托(delegate)是如何工作的?

标签 ios objective-c uitableview

我是 ios 开发的新手。当我使用 UITableView 时,我将数据源和委托(delegate)一起实现。比如下面两种方法:

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

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

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

还有一个问题,什么情况下才用数据源?因为我们可以创建自定义委托(delegate)吗?有没有我们只创建自定义数据源的情况?因为我看到数据源总是带有委托(delegate)....谢谢!!!

最佳答案

UITableViewDataSource 协议(protocol)定义了 UITableView 需要用数据填充自身的方法。它定义了几个可选的方法,但有两个是必需的(不是可选的):

// 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:

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

// 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:

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

您所说的:“(该)数据源方法已运行 10 次以获取行数。”不是真的。数据源方法–tableView:numberOfRowsInSection: 没有运行10次获取行数。事实上这个方法只运行一次。此外,此方法在 –tableView:cellForRowAtIndexPath: 之前运行,因为 TableView 需要知道它必须显示多少行。

最后,方法 –numberOfSectionsInTableView: 也运行一次,它在 –tableView:numberOfRowsInSection: 之前运行,因为 TableView 需要知道有多少个部分。请注意,此方法不是必需的。如果您不实现它, TableView 将假定只有一个部分。

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

UITableViewDelegate 的一些最常见的方法是:

// 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:

希望这对您有所帮助!

关于ios - UITableView 中的数据源和委托(delegate)是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501169/

相关文章:

ios - 如何用百分比颜色填充 UITableViewCell

ios - 当应用程序处于前台时,我们如何显示推送通知?

ios - 具有周、日和月 View 的 iOS 日历

iphone - iPhone 的 MAC 地址是唯一的,它会改变吗?

ios - mas_updateConstraints 没有移除 Masonry 中的约束

ios - 从 UITableViewCell 内的 UI 元素收集数据

ios - UITableView didSelectRowAtIndexPath 未被调用

ios - ViewController 的高度设置不正确

iphone - 将对象从NSMutableDictionary添加到NSMutableArray

ios - UITableView contentOffset 行为异常