【问题标题】:Trying to up UIViewController and MapKit尝试启动 UIViewController 和 MapKit
【发布时间】: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
}

结果:

【问题讨论】:

    标签: swift xcode uikit mapkit


    【解决方案1】:

    尝试在外部函数中进行此调用,例如: 每次调用时,如果有结果,请更新您的数组,然后在最后调用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
    

    【讨论】:

    • 天哪,它奏效了……你介意解释一下为什么我所做的没有奏效吗?我现在已经坚持了大约 6 个小时。
    • 另外,我想提一下,以供将来查看此内容的任何人参考,在 cellForRowAt 方法中,您需要将单元格更改为可选,从而导致:guard let cell = self.tableView.dequeueReusableCell( withIdentifier: "cell") as? CustomTableViewCell else {return UITableViewCell()} 您还需要将设置标签名称的行包含在 if 函数中,以确保 matchingItems 持有一个值,否则您将收到超出范围的错误: if(matchingItems.count > 0){ cell.nameLabel.text = matchingItems[indexPath.row].placemark.name }
    • 我很高兴它成功了,cellForRowAt,不是进行搜索调用或任何更新数据的好地方。 cellForRowAt 方法只是部署单元格的地方,并告诉表格视图您将如何将单元格的子视图与数据绑定。这就是为什么,你应该在另一个方法中进行任何更新,一旦你更新了你的数据源,然后通过调用 tableView.reloadData,你现在让 tableView 更新了数据源,并且在 cellForRowAt 方法中为新数据重新创建了单元格。 @cdunn2013
    • 非常感谢...我会记住的。你是救生员!
    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2015-01-20
    • 2014-11-27
    相关资源
    最近更新 更多