【问题标题】:UITableView reloadData loopingUITableView reloadData 循环
【发布时间】:2018-07-30 11:15:01
【问题描述】:

在我的应用中,我正在根据用户的位置更新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()
            }
        }
    }
}

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 别想了:确保self.tableView.reloadData() 正在主线程上执行
  • 好主意,我确实添加了它,但它没有任何改变。 ??????
  • 确保您的表视图委托或数据源函数中没有任何reloadData()

标签: swift uitableview cllocationmanager


【解决方案1】:

您需要在找到位置后停止更新您的位置。重新加载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()
            }
        }
    }
}

【讨论】:

  • 这也不是问题。如果位置不同,我只会重新加载,而且:添加您的代码并不能解决问题。
  • 你能再贴一些代码吗?也许您正在其他地方调用 reloadData()
猜你喜欢
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2011-11-24
  • 2019-06-12
  • 1970-01-01
相关资源
最近更新 更多