在我的应用程序中,我正在根据用户的位置更新UITableView。我在CLLocationManagerDelegate中做这件事。现在,发生的情况是,我的UITableView将不断重新加载,因此不会绘制任何内容。当我注释掉reloadData()调用时,一切都很好(当然没有位置)。当我重新添加它时,print()被执行一次。在此之后,表将继续重新加载。
extension RestaurantsViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locations.count > 0) {
if (location?.coordinate.longitude != locations[0].coordinate.longitude && location?.coordinate.latitude != locations[0].coordinate.latitude) {
location = locations[0]
print("will reload table")
self.tableView.reloadData()
}
}
}
}你知道我该怎么解决这个问题吗?
发布于 2018-07-30 19:38:31
你需要在找到你的位置后使用stopUpdating。重新加载tableView后添加此manager.stopUpdatingLocation()
extension RestaurantsViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locations.count > 0) {
if (location?.coordinate.longitude != locations[0].coordinate.longitude && location?.coordinate.latitude != locations[0].coordinate.latitude) {
location = locations[0]
print("will reload table")
self.tableView.reloadData()
manager.stopUpdatingLocation()
}
}
}
}https://stackoverflow.com/questions/51592741
复制相似问题