无法使用 swift 3 从其数据源获取单元格 [英] failed to obtain a cell from its dataSource with swift 3
问题描述
我将以下 UITableViewController 作为 searchResultsController 附加:
I have the below UITableViewController attached as a searchResultsController:
import UIKit
import MapKit
class LocationSearchTable : UITableViewController {
var matchingItems:[MKMapItem] = []
var mapView: MKMapView? = nil
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController){
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
extension LocationSearchTable {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(matchingItems.count)
return matchingItems.count
}
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
}
}
这里它崩溃一次 matchingItems.count
不等于 0 例如:
Here it crashed once matchingItems.count
is no equal to 0 for example:
0 0 10 2016-10-20 15:43:53.914 ios-App[9137:977735] * 断言-[UITableView _configureCellForDisplay:forIndexPath:] 中的失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:80352016-10-20 15:43:53.927 ios-App[9137:977735] * 终止应用到期未捕获的异常NSInternalInconsistencyException",原因:'UITableView (; layer = ; contentOffset: {0,-64};contentSize: {768, 440}>) 未能从其数据源获取单元格
0 0 10 2016-10-20 15:43:53.914 ios-App[9137:977735] * Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:8035 2016-10-20 15:43:53.927 ios-App[9137:977735] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, -64}; contentSize: {768, 440}>) failed to obtain a cell from its dataSource
当我在 cellForRowAtIndexPath 中添加调试点时,它们永远不会到达,应用程序似乎在此之前崩溃?
When I add debugging points witin cellForRowAtIndexPath they are never reached, the app seems to crash before this point?
推荐答案
错误
未能从其数据源获取单元格
failed to obtain a cell from its dataSource
发生是因为cellForRowAtIndexPath
的签名错误.在 Swift 3 中是
occurs because the signature of cellForRowAtIndexPath
is wrong. In Swift 3 it's
(override) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
并使用方便的方法
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
它总是返回一个有效的非可选单元格.
which returns always a valid non-optional cell.
PS:您不需要将数据源和委托设置为 self
因为 UITableViewController
隐式地这样做
PS: You don't need to set datasource and delegate to self
because UITableViewController
does that implicitly
这篇关于无法使用 swift 3 从其数据源获取单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!