如何从转义闭包中获取值并将其分配给变量并显示在 tableview 中

迪兰凯

我使用 CoreLocation 计算了距离,但无法将距离分配给变量并在 Tableview 中显示该值。

我使用 forwardGeocoding 函数将 String 转换为 CLLocation 类型并获得两个点之间的距离。我可以打印闭包内的数据,但我永远无法将距离分配给闭包外的变量。

// This function will accept a string and convert to CLLocation
func forwardGeocoding(address:String, completion: @escaping ((CLLocation?) -> Void))
{
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address, completionHandler: { (placemarks, error) in
        if let error = error
        {
            print(error)
            completion(nil)
        }
        else
        {
            // found locations, then perform the next task using closure
            let placemark = placemarks?[0]
            completion(placemark?.location) // execute closure
        }
    })
}

// This function will find the distance, assign it to a local variable and return it 
func searchDistance(_ address: String) -> Double
{
    var distance: Double = 0
    self.forwardGeocoding(address:address, completion: {(location) in
        if let locationTo = location {
            // calculate distance
            distance = (self.mainDelegate.currLocation?.distance(from: locationTo))!
            // distance has value here
            print("distance in closure \(distance)")

        } else {
            // not found destination, show alert
            print("cannot find address")
        }
    })
    print(distance) // fails to catch the distance, always 0
    return distance      
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let tableCell = tableView.dequeueReusableCell(withIdentifier: "DonorCell", for: indexPath) as? DonorListTableViewCell ?? DonorListTableViewCell(style: .default, reuseIdentifier: "DonorCell")

    let index = indexPath.row
    tableCell.donorName?.text = donors[index].organizationName
    tableCell.donorAddress?.text = "\(donors[index].street ?? ""), \( donors[index].city ?? "")"
    tableCell.donorPhone?.text = donors[index].phoneNumber

    let donorAddress = "\(donors[index].street ?? "") \(donors[index].city ?? "") \(donors[index].postalCode ?? "")"

    // This is always 0
    let distanceOnMap = searchDistance(donorAddress)

    return tableCell
}

我认为这是@escaping 问题,但我不知道如何修改它以成功显示表格单元格中的距离。请帮忙

迪兰凯

问题在我老师的帮助下解决了。解决方法是创建一个实例变量,然后在闭包中直接调用self.tableview.reload。在 tableView cellForRowAT 中,只需从实例变量加载值。

func searchAddress(_ address: String)
{
    self.forwardGeocoding(address:address, completion: {(location) in
        if let locationTo = location {
            // calculate distance
            self.donorDistance.append((self.mainDelegate.currLocation?.distance(from: locationTo) ?? 0))
            print(self.mainDelegate.currLocation?.distance(from: locationTo) ?? 0)
            if self.donorDistance.count == self.donors.count
            {
                self.tableview.reloadData()
            }              
        } else {
            // not found destination, show alert
            print("cannot find address")
        }
    })
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从文本中获取值并将其分配给变量

C#-从JSON响应中获取值并将其分配给变量

如何获取活动按钮的ref属性并将其分配给vue.js中的变量

在Makefile中,如何获取git commit哈希并将其分配给变量?

您如何从html文本框中获取输入并将其分配给javascript变量?

如何获取在TableView单元格中显示的数据并将其显示在文本字段中?

如何从文件中提取值(在特定的行和列中)并将其分配给新变量

如何从PFObject中获取String并将其显示在tableView单元格中?

从输入中读取整数并将其分配给变量

从数组中调用元素并将其分配给变量?

在 tableView 中显示列

在tableView中显示数组

如何从诺言响应中获取值并将其分配给Ionic v3中数组中的每个对象属性

从网站的 HTML 中获取单个数据并将其分配给变量

iOS TableView如何更新TableView中的元素

如何将sql查询的结果分配给变量并将其用于内置函数中?

如何解析JSON信息然后选择信息并将其分配给Swift中的变量

如何从MySQL中读取双列并将其分配给C#双变量?

如何从文件中读取数组并将其分配给变量?

如何从字符串中提取单词并将其分配给Python 3.7中的变量?

如何从 URL 读取 JSON 文件并将其分配给 NodeJS 中的全局变量?

如何查找文件的大小并将其分配给UNIX中的变量

如何从组件中获取变量值并将其分配给 vuejs 中另一个组件中的变量

如何从tableview中的绑定文本字段中获取值?

如何在fullcalendar v5的eventConten中获取元素并将其分配给tip?

如何从ParamMap中获取route参数并将其分配给属性

Firebase中的Tableview单元

JavaFX的从TableView中删除

collectionView IOS中的tableView