尝试启动 UIViewController 和 MapKit

Trying to up UIViewController and MapKit

所以我正在尝试制作一个日历应用程序。在此应用程序中,它允许用户在他们的事件中放置一个位置以获取旅行时间(类似于 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
}

结果:

尝试在外部函数中进行此调用,例如: 每次调用时,如果有结果,请更新数组,然后在最后调用 tableView.reloadData(),这样它会为您创建必要的单元格。

func makeSearchCall() {
    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)
            tableView.reloadData()
        }
    })
}

然后在您的 cellForRowAt 方法中:

guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as? CustomTableViewCell else {return UITableViewCell()}
cell.nameLabel.text = matchingItems[indexPath.row].placemark.name
return cell

也在你的numberOfRowsInSection方法中:

return matchingItems.count