Swift 3.0 UITableViewDelege Objective-c 方法与要求的选择器不匹配

标签 swift swift3

我最近使用 Xcode 8.0 将一个项目转换为 Swift 3,但我在一个我不太了解的函数上遇到了错误。在这些方面:

extension HomeTableViewController : UITableViewDelegate {

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    }
}

为了解决这个错误,Xcode 告诉我在方法之前添加 @objc(tableView:commitEditingStyle:forRowAtIndexPath:)

Xcode error

好的,它可以工作,但我不明白为什么只有这种方法才需要它。

Xcode 不需要在我的 tableView:heighForHeaderInSection 前面添加 @objc 东西,但我没有看到此方法和 UITableViewDelegate 之间的任何差异tableView:commitEditingStyle:forRowAtIndexPath:

那么,知道为什么这对于 tableView:commitEditingStyle:forRowAtIndexPath 方法是强制性的吗?

提前致谢! 😉

最佳答案

您在扩展程序中采用了不正确的协议(protocol)。 tableView:commitEditingStyle:forRowAtIndexPath: 方法是 UITableViewDataSource 协议(protocol)的一部分。更改您的扩展以采用 UITableViewDataSource 协议(protocol)而不是 UITableViewDelegate 协议(protocol),错误消失。

至少当我在 NSCollectionViewDelegate/DataSource 扩展中遇到这个错误时,这对我有用。

关于Swift 3.0 UITableViewDelege Objective-c 方法与要求的选择器不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510890/

相关文章:

ios - Swift:IBOutlets 在 Present Modally segue 之后为零

ios - 将日期转换为格式为 "yyyy-MM-dd' T'HH :mm:ssZ"的字符串

swift - 有没有办法将 GLB 文件下载到 ArKit/RealityKit 应用程序中?

ios - CGPointZero 和 CGContextDrawImage 不可用

ios - 在 Swift 3 中读取 JSON 文件时出错

ios - 如何创建具有动态数量标签的自定义 UITableViewCell

ios - 将字符串保存在一个地方的最佳实践

ios - 火力地堡谷歌认证( swift )

ios - swift 3 : How do you save a high score programmatically?

swift - Firebase 检查帐户是否被禁用