我正在创建一个带有自动完成搜索栏的 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 {
我做错了什么?谢谢!
最佳答案
您可能需要澄清一些事情。
我假设:
您的
LocationSearchTable
是UITableViewController
的子类。您使用 Xcode 8/Swift 3 或更高版本。
您不会收到以下行的错误或警告:
覆盖 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分:Int) -> Int {
而且你应该更好地展示你从哪里得到显示错误的代码,它看起来非常非常旧。
最好找到 UITableViewDataSource
的方法在最新的 Swift 中是什么样子的,你知道,UITableViewController
符合:
...
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/