无法使用swift 3从其数据源获取单元格
我已将以下UITableViewController作为searchResultsController连接:无法使用swift 3从其数据源获取单元格,swift,uitableview,Swift,Uitableview,我已将以下UITableViewController作为searchResultsController连接: import UIKit import MapKit class LocationSearchTable : UITableViewController { var matchingItems:[MKMapItem] = [] var mapView: MKMapView? = nil override func viewDidLoad() {
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,例如:
0 0 10 2016-10-20 15:43:53.914 ios应用程序[9137:977735]*断言
-[UITableView\u 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应用程序[9137:977735]*终止应用程序到期
要取消捕获异常“NSInternalInconsistencyException”,原因:
'UITableView(;层=;内容偏移:{0,
-64};contentSize:{768440}>)无法从其数据源获取单元格
当我在cellForRowAtIndexPath中添加调试点时,它们从未到达,应用程序似乎在此点之前崩溃?错误
无法从其数据源获取单元格
由于cellForRowAtIndexPath
的签名错误而发生。在Swift 3中是
(override) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
并使用方便的方法
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
它始终返回一个有效的非可选单元格
PS:您不需要设置数据源并将其委托给
self
,因为UITableViewController
会隐式地这样做我也有类似的问题!经过几分钟的调试,我意识到我没有继承
UITableViewDelegate,UITableViewDataSource
在我的ViewController上。需要检查几件事:1)确保表视图的数据源正确设置为视图控制器。2) 请确保您已在表视图中注册了单元格,以便它们可以正确地退出队列。谢谢@dlbuckley我已更新了question@dlbuckley如果
UITableViewController
是在Interface Builder中设计的–很明显–您不需要注册单元格,也不需要设置delegate
和datasource
编程。另一个问题。调用dequeueReusableCell
可以返回nil
(override)func tableView(u.tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell给我一个错误(预期声明
和连续声明必须用“;”分隔)
)。Xcode 8.1.@afkatja我将覆盖放在括号中。如果目标类是UITableViewController
,则需要override
,如果目标类是UIViewController
,则不需要它。删除括号,并遵循编译器的建议(如果有)。