table 视图控制器中的函数是如何调用的?
How are functions in table view controllers called?
我是使用 Swift 进行 iOS 开发的新手。
我想了解如何在控制 table 视图的视图控制器中调用函数。
在我正在查看的示例中,视图控制器运行三个函数,所有函数都称为 'table view',并且每个函数都做一些独特的事情,例如返回一个部分中有多少行,或使用可重用的单元格。
但我就是看不到这些函数是何时以及如何被调用的。
当用户导航到视图时会调用它们吗?如果是这样,如何?为什么这些不同的函数都具有相同的名称(即 func tableView ()
)?
下面是一些示例代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dwarves.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier)
as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: simpleTableIdentifier)
}
cell!.textLabel.text = dwarves[indexPath.row]
cell!.textLabel.font = UIFont .boldSystemFontOfSize(15)
return cell!
}
I just can't see when or how these functions are called.
A UITableView
在其 delegate
或 dataSource
上调用这些方法以获取有关应显示内容的信息,并在特定操作发生时进行通信。对于 UITableViewController
,控制器本身既是委托又是数据源。因此,除非您在其中设置断点,否则您不会看到这些方法被调用。
Are they called when the user navigates to the view? If so, how?
当用户导航到视图时,UITableViewController
的默认实现将 table 视图的委托和数据源属性设置为 self
。 table 视图本身在需要信息来适当地创建、调整大小、布局和显示 table 单元格时调用这些方法。
And how come these different functions all have the same name? I.e func tableView ()
他们没有。在 Swift 和 Objective-C 中,参数名称都是方法名称的一部分。比如这个方法:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
被命名为tableView(_:heightForRowAtIndexPath:)
。
我是使用 Swift 进行 iOS 开发的新手。
我想了解如何在控制 table 视图的视图控制器中调用函数。
在我正在查看的示例中,视图控制器运行三个函数,所有函数都称为 'table view',并且每个函数都做一些独特的事情,例如返回一个部分中有多少行,或使用可重用的单元格。
但我就是看不到这些函数是何时以及如何被调用的。
当用户导航到视图时会调用它们吗?如果是这样,如何?为什么这些不同的函数都具有相同的名称(即 func tableView ()
)?
下面是一些示例代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dwarves.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier)
as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: simpleTableIdentifier)
}
cell!.textLabel.text = dwarves[indexPath.row]
cell!.textLabel.font = UIFont .boldSystemFontOfSize(15)
return cell!
}
I just can't see when or how these functions are called.
A UITableView
在其 delegate
或 dataSource
上调用这些方法以获取有关应显示内容的信息,并在特定操作发生时进行通信。对于 UITableViewController
,控制器本身既是委托又是数据源。因此,除非您在其中设置断点,否则您不会看到这些方法被调用。
Are they called when the user navigates to the view? If so, how?
当用户导航到视图时,UITableViewController
的默认实现将 table 视图的委托和数据源属性设置为 self
。 table 视图本身在需要信息来适当地创建、调整大小、布局和显示 table 单元格时调用这些方法。
And how come these different functions all have the same name? I.e
func tableView ()
他们没有。在 Swift 和 Objective-C 中,参数名称都是方法名称的一部分。比如这个方法:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
被命名为tableView(_:heightForRowAtIndexPath:)
。