表视图数据源值更新问题



我有一个应用程序,用户可以在其中从搜索栏搜索位置。当用户搜索任何位置时,结果将显示在搜索栏下的表视图中。问题来了,当我搜索任何位置时,它会给出结果,但没有显示在表格视图中。我有检查委托和数据源,它们已正确附加,但表视图仍然没有显示任何数据。如何在表视图中获取该数据。我使用了断点,它也没有捕获断点。这是我获取搜索结果的代码,

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)";

}]

这是我的 swift 文件的完整代码,

// 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
}

您需要在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
}

最新更新