我是使用Swift开发iOS的新手。如何调用表视图控制器中的函数?
我想了解如何在控制表视图的视图控制器中调用函数。
在我看的例子中,视图控制器运行三个函数,都称为'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!
}
您可以添加一个链接到您正在查看的示例吗? – gaborous
我没有链接,对不起。这些例子都在一本书中。 –
我已经添加了一些示例代码来说明。 –