我想在收到位置更新时更新我的 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/