OGeek|极客世界-中国程序员成长平台

标题: ios - swift 3 : Understand syntax change for UITableViewDataSource method [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:31
标题: ios - swift 3 : Understand syntax change for UITableViewDataSource method

我对 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 数据源方法时才会发生这种情况。 numberOfRowsInSectionviewForHeaderInSection 也不会发生这种情况。

谁能帮我理解为什么会这样?



Best Answer-推荐答案


虽然我不确定是什么触发了 @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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4