ios - Swift - 覆盖 tableview 函数 - 方法不会覆盖其父类(super class)中的任何方法

标签 ios swift mapkit uisearchcontroller mklocalsearchrequest

我想实现 UISearchController,使用本教程逐步进行:

Search for places using MKLocalSearchRequest and display results with UISearchController

在这个部分...

Search for locations using MKLocalSearchRequest

...我对第 3 步有疑问。设置 TableView 数据源

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.dequeueReusableCellWithIdentifier("cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
  }
}

Error:Method does not override any method from its superclass

我正在尝试删除 override来自 cellForRowAtindexPath tableView函数,在本例中我在Xcode中没有报错,但是执行后报错:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView (<UITableView: 0x7ff8e03a3800; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: 0x604002046450>; layer = <CALayer: 0x604001626620>; 
contentOffset: {0, -56}; contentSize: {375, 132};adjustedContentInset: {56, 0, 0, 0}>) 
failed to obtain a cell from its dataSource(<CityWeather.LocationSearchTable: 0x7ff8de696e60>)'

错误消息大喊:failed to obtain a cell from its dataSource(<CityWeather.LocationSearchTable: 0x7ff8de696e60>)

请告诉我,我做错了什么?我该如何处理这个问题?谢谢。

最佳答案

tutorialUITableviewDatasource 使用旧语法方法(可能是 Swift 2.2):

你现在需要这些:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

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

}

尽可能始终使用 XCode 的代码完成。

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

相关文章:

输入多个单词时,Swift UIText 字段抛出 nil 错误

swift - MapKit 从坐标绘制折线 Swift 5

iOS 6 - 如何在方向改变时运行自定义代码

ios - 我想要一个全局函数,它调用我整个应用程序中的每个按钮单击。这应该是什么小技巧?

ios - 使用 Swift 返回具有自定义数据类型的 JSON 数据时遇到问题

ios - 两个物体之间的碰撞

objective-c - 将 MapView 委托(delegate)设置为 self 时 showUserLocation 发生变化

c# - 如何删除 UIButton 设置的默认约束?

ios - Cocoapods 默认安装旧版本的 Stripe 库

swift - 如何在 Swift 中声明计算属性 'throws'?