swift - TableView 数据源值更新问题
问题描述
我有一个应用程序,用户可以在其中从搜索栏中搜索位置。当用户搜索任何位置时,结果会显示在搜索栏下方的表格视图中。问题来了,当我搜索任何位置时,它会给出结果,但不会显示在表格视图中。我检查了委托和数据源,它们已正确连接,但表格视图仍然没有显示任何数据。我怎样才能在表格视图中获取该数据。我已经使用了断点,它也没有捕捉到断点。这是我如何获取搜索结果的代码,
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
}
推荐阅读
- android - 与标头一起使用时 C++ Crashlytics NDK 崩溃
- python - 在字典中的两个键之间交换值
- jenkins - Jenkins 声明式管道部署批准仅适用于一个环境
- java - 如何在 intellij 中获取项目名称作为变量以用于 XML 文件?
- javascript - 这是一个有效的箭头函数吗?
- python - Plotly Dash:从二叉树拆分动态创建下拉列表
- php - fopen() 无法打开和附加文件
- c++ - 原子变量是否仅在其所在的缓存行上刷新缓存?
- amazon-web-services - AWS ALB 504 网关超时
- npm - 如何防止 NPM 从 node_modules 中删除本地安装的模块