ios - swift 3 : Understand syntax change for UITableViewDataSource method

标签 ios uitableview swift3

我对 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 也不会发生这种情况。

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

最佳答案

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

相关文章:

ios - 如何在iOS中开发移动设备管理应用程序

javascript - Firebase 推送给了我 SyntaxError : invalid object initializer

ios - Xcode - 为什么 "Embedded Binaries"下没有 "General"?

ios - 单击tableview Cell中的Collectionview时如何找到tableview的索引路径

ios - CGContextSetShadow UITableViewCell 性能

ios - 在reloadData Xcode的同时,在表格末尾插入新的自定义行

swift - 函数不会调用 Swift 3

ios - 处理 UILabel 中的触摸事件并将其连接到 IBAction

ios - 我想在 iOS Swift 中摇动谷歌地图标记

swift - 将 Swift 数组转换为带索引的字典