【发布时间】:2018-12-24 23:12:51
【问题描述】:
所以我正在尝试制作一个日历应用程序。在这个应用程序中,它允许用户在他们的事件中放置一个位置作为旅行时间(类似于 iOS 地图的操作方式。)但是,我在填充 TextField 下的 ListView 时遇到了问题......下面是代码和结果。
更新 ListView 的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = locationSearchTextField.text
request.region = mapKitView.region
let search = MKLocalSearch(request: request)
search.start(completionHandler: {(response, error) in
if error != nil{
print("Error occured during search:")
print(error!.localizedDescription)
numberOfResults = 0
tableView.isHidden = true
return
}else if response!.mapItems.count == 0{
print("No results found")
numberOfResults = 0
tableView.isHidden = true
return
}else{
print("Results found")
matchingItems = (response?.mapItems)!
numberOfResults = matchingItems.count
tableView.numberOfRows(inSection: numberOfResults)
print(numberOfResults)
let indexRow = indexPath.row
let matchingItemsCount = matchingItems.count
if(matchingItemsCount > indexRow){
cell.nameLabel.text = matchingItems[indexRow].placemark.name
}else{
return
}
}
})
return cell
}
跟踪用户何时编辑 TextField 的代码(此代码用于更新 ListView):
@IBAction func locationTextFieldChanged(_ sender: Any) {
tableView.reloadData()
tableView.isHidden = false
}
【问题讨论】: