我对 Swift 3 函数调用有一些疑问。下面是一个例子。
老 swift :
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell
swift 3:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
上面的语法没问题。但是现在 Xcode 向我显示错误并要求我执行以下操作:
@objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
我不明白为什么我必须声明 @objc(tableView:cellForRowAtIndexPath:)
。
只有当我尝试在扩展下实现 TableView 数据源方法时才会发生这种情况。此外,numberOfRowsInSection
或 viewForHeaderInSection
也不会发生这种情况。
谁能帮我理解为什么会这样?
最佳答案
虽然我不确定是什么触发了 @objc
,但我可以建议采用以下方法:
将 tableView 变量存储在 viewDidLoad 的某处:
let tv = tableView!
然后将鼠标悬停在 tableView 变量上并按下命令按钮并单击。
这会将您带到 UITableView 的界面。
然后,将鼠标悬停在 UITableViewDelegate 或 UITableViewDataSource 上并按下命令按钮并单击。
现在您可以看到新的签名。
发生了很多变化...升级愉快!
关于ios - swift 3 : Understand syntax change for UITableViewDataSource method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39519709/