ios - 方法不覆盖其父类(super class)中的任何方法 : Swift/UITableViewController/MapKit

标签 ios swift

我正在创建一个带有自动完成搜索栏的 mapView,但我收到了一个Method Does Not Override Any Method From Its Superclass 错误。这是我发生此错误的代码示例:

extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    return matchingItems.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath 
indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
}
}

这是创建错误的函数:

override func tableView(tableView: UITableView, cellForRowAtIndexPath 
indexPath: NSIndexPath) -> UITableViewCell { 

我做错了什么?谢谢!

最佳答案

您可能需要澄清一些事情。

我假设:

  • 您的 LocationSearchTableUITableViewController 的子类。

  • 您使用 Xcode 8/Swift 3 或更高版本。

  • 您不会收到以下行的错误或警告: 覆盖 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分:Int) -> Int {

而且你应该更好地展示你从哪里得到显示错误的代码,它看起来非常非常旧。

最好找到 UITableViewDataSource 的方法在最新的 Swift 中是什么样子的,你知道,UITableViewController 符合:

UITableViewDataSource

...

func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell

...

导入的方法名称在最近的 Swifts 中更加快速化,使用旧代码片段时应小心。

尝试将方法头更改为:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

请不要忘记第一个参数的空标签_,第二个参数标签是cellForRowAt,不是cellForRowAtIndexPath,其类型是 IndexPath,而不是 NSIndexPath

另外,如果您删除 override,您可能会收到错误消息。

关于ios - 方法不覆盖其父类(super class)中的任何方法 : Swift/UITableViewController/MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50826432/

相关文章:

swift - 将 Set 转换为 Realm MutableSet

ios - tableView.reloadData() 每次 View 出现

ios - 在 iOS 上的 UINavigationItem 上设置 2 行标题?

ios - 线程 1 : signal SIGABRT in AppDelegate

ios - 使用 objective-c/core graphics 的水平居中文本

iphone - 如何让 asl_log 出现在 iOS 系统控制台输出中?

swift - 嵌套函数使 Swift 编译器崩溃

ios - viewController 中出现信号 SIGABRT 错误

ios - Swift opencv编译错误 'list'找不到文件

ios - Firebase 更改显示在谷歌登录警报上的应用程序名称?