Swift - 重写 tableview 函数 - 方法不重写其超类中的任何方法
Swift - override tableview function - Method does not override any method from its superclass
我想实现 UISearchController,使用本教程逐步进行:
Search for places using MKLocalSearchRequest and display results with UISearchController
并且在这个部分...
Search for locations using MKLocalSearchRequest
...我在 3.Set 上 Table 查看数据源
时遇到问题
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
}
}
我正在尝试从 cellForRowAtindexPath
tableView 函数中删除 override
,在这种情况下,我在 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>)
请告诉我,我做错了什么?我该如何处理这个问题?谢谢。
tutorial is using old syntax for UITableviewDatasource 方法(可能是 Swift 2.2):
你现在需要这些:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
尽可能使用 XCode 的代码补全。
正如 Puneet Sharma 所说,您不需要覆盖方法。因为您在 UIViewController
.
中使用 UITableViewDatasource
(和 UITableViewDelegate
)
您需要在 ViewController
中设置 tableView
的委托。
我想实现 UISearchController,使用本教程逐步进行:
Search for places using MKLocalSearchRequest and display results with UISearchController
并且在这个部分...
Search for locations using MKLocalSearchRequest
...我在 3.Set 上 Table 查看数据源
时遇到问题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
}
}
我正在尝试从 cellForRowAtindexPath
tableView 函数中删除 override
,在这种情况下,我在 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>)
请告诉我,我做错了什么?我该如何处理这个问题?谢谢。
tutorial is using old syntax for UITableviewDatasource 方法(可能是 Swift 2.2):
你现在需要这些:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
尽可能使用 XCode 的代码补全。
正如 Puneet Sharma 所说,您不需要覆盖方法。因为您在 UIViewController
.
UITableViewDatasource
(和 UITableViewDelegate
)
您需要在 ViewController
中设置 tableView
的委托。