swift - 收到位置更新时更新 TableView Cell

标签 swift tableview

我想在收到位置更新时更新我的​​ TableView,从而更新我向用户显示的距离标签。我创建了一个通知来通知我更新。

self.notificationCenter.addObserver(self, selector: "locationAvailable:", name: "LOCATION_AVAILABLE", object: nil)

func locationAvailable(notification: NSNotification) {
    let userInfo = notification.userInfo as! Dictionary<String, AnyObject>
    self.currentLocation = userInfo["location"] as! CLLocation
}

tableView 单元格填充到数据源中:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AEDTableViewCell

        let aed: AED = self.aeds[indexPath.row]

        // Configure the cell
        cell.streetName.text = aed.street!
        cell.owner.text = aed.owner!
        cell.distanceLabel.text = String(format: "%.0f" + "m", calculateDistance(currentLocation: self.currentLocation, latitude: aed.latitude!, longitude: aed.longitude!))

        return cell
    }

我通过方法calculateDistance计算距离

calculateDistance(currentLocation: self.currentLocation, latitude: aed.latitude!, longitude: aed.longitude!)

但是只要没有接收到当前位置,我的tableview中就有一个错误的值。

如何在收到通知后立即更新 tableView 中的不同单元格?

最佳答案

最简单的方法是在 locationAvailable 中执行 tableView.reloadData()

关于swift - 收到位置更新时更新 TableView Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36220399/

相关文章:

swift - 将快照解析为模型 - FirebaseUI

swift - 分别展开每个 collectionView 单元格

swift - UITableVC 未显示节标题内容

swift - 在数组中添加一个按钮 - Swift

swift - NStableView 不显示我的数据

swift - 如何将json对象添加到字典

Swift - 自动在刚刚添加的按钮右侧添加一个按钮

swift - iOS 应用程序框架崩溃

swift - 从保存在表格 View 中的已发送电子邮件中获取电子邮件和时间戳

Swift: tableView.reloadData() 表中没有数据