我有一个应用程序,用户可以在其中从搜索栏搜索位置。当用户搜索任何位置时,结果将显示在搜索栏下方的表格视图中。问题来了,当我搜索任何位置时,都会给出结果,但不会显示在表格视图中。我有检查委托和数据源,它们已正确连接,但表视图仍未显示任何数据。我如何在表格视图中获取该数据。我用过断点,也没有抓住断点。这是我如何获取搜索结果的代码,
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
print(searchBarText)
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
print(search)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
print(self.matchingItems)
self.tableView.reloadData()
}
}
在这里,我要将搜索结果传递给表格视图,
extension LocationSearchTable {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
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
print( cell.textLabel?.text)
cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
print( cell.detailTextLabel?.text)
return cell
}
} 这是我搜索任何位置时出现的响应,
[<MKMapItem: 0x1c434d5d0> {
isCurrentLocation = 0;
name = California;
placemark = "California, California, United States @ <+37.13374180,-120.28640480> +/- 0.00m, region CLCircularRegion (identifier:'<+37.41896824,-119.30660700> radius 706259.58', center:<+37.41896824,-119.30660700>, radius:706259.58m)";
timeZone = "America/Los_Angeles (GMT-7) offset -25200 (Daylight)";
}]
这是我的快捷文件的完整代码
// weak var delegate: HandleMapSearch?
var matchingItems: [MKMapItem] = []
var mapView: MKMapView?
func parseAddress(selectedItem:MKPlacemark) -> String {
// put a space between "4" and "Melrose Place"
let firstSpace = (selectedItem.subThoroughfare != nil &&
selectedItem.thoroughfare != nil) ? " " : ""
// put a comma between street and city/state
let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) &&
(selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
// put a space between "Washington" and "DC"
let secondSpace = (selectedItem.subAdministrativeArea != nil &&
selectedItem.administrativeArea != nil) ? " " : ""
let addressLine = String(
format:"%@%@%@%@%@%@%@",
// street number
selectedItem.subThoroughfare ?? "",
firstSpace,
// street name
selectedItem.thoroughfare ?? "",
comma,
// city
selectedItem.locality ?? "",
secondSpace,
// state
selectedItem.administrativeArea ?? ""
)
return addressLine
}
答案 0 :(得分:1)
您需要在viewDidLoad
tableView.delegate = self
tableView.dataSource = self
并像这样正确实现方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {t {
return matchingItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
print( cell.textLabel?.text)
cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
print( cell.detailTextLabel?.text)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}