如何调用方法两次?

时间:2014-10-09 13:20:09

标签: ios swift

我正在快速学习并在课堂上使用以下方法:

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return countries.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel!.text =  countries[indexPath.row]
    return cell
}

这让我感到困惑,因为我之前没有编程,我可以有一个可以被调用两次的方法(即tableView)。这怎么可能?

2 个答案:

答案 0 :(得分:5)

您不应该认为该方法仅由括号前的名称定义。如果参数不同,那么它就是一种完全不同的方法。

答案 1 :(得分:1)

这些不是方法调用;这些是函数定义。但是,因为它们共享相同的函数名称,所以在不指定参数名称的情况下不能调用它们。调用它们将是这样做的(虽然它是在UITableView内部完成的,所以你不必在这种特殊情况下担心它):

tableView(theTableView, numberOfRowsInSection:theSection);
tableView(theTableView, cellForRowAtIndexPath:theIndexPath);

请注意,sectionindexPath分别是函数内部用于外部参数名称numberOfRowsInSectioncellForRowAtIndexPath的名称。

有关签名的函数名称,参数和一般结构的更多信息,请参阅此处的Swift文档:https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_598