gpt4 book ai didi

swift - CellForRow 被延迟调用

转载 作者:行者123 更新时间:2023-11-30 10:11:09 24 4
gpt4 key购买 nike

每当用户位置发生显着变化时,我都会尝试从网络服务器加载数据。我的问题是,对于任何位置更改,tableview 都会调用“numberOfRows”,但它会等待 30 秒,直到调用“cellForRowAtIndexPath”。或者直到用户移动表格。

我有时还会遇到随机警告,表明自动布局不受主队列线程的影响(这可能与此问题有关)

我的问题出在哪里?也许我异步做两次事情不好?

这是我的代码:

import UIKit
import MapKit

class GroundListTVCTEST: UITableViewController,CLLocationManagerDelegate, UISearchBarDelegate, UISearchControllerDelegate
{

var locationManager = CLLocationManager()
var searchController :TKSearchController!
var localSearchRequest :MKLocalSearchRequest!
var localSearch :MKLocalSearch!
var localSearchResponse :MKLocalSearchResponse!
var grounds:[Ground]? {
didSet {
self.tableView.reloadData()
}
}

override func viewDidLoad() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startMonitoringSignificantLocationChanges()
let distance:CLLocationDistance = 1000.0
locationManager.distanceFilter = distance
}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.locationManager.startUpdatingLocation()

print("Location Manager started.")

}

override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
locationManager.stopUpdatingLocation()

print("Location Manager stopped.")

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("cellForRow called")
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
cell?.textLabel?.text = grounds?[indexPath.row].name
return cell!
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("number of rows called")
return grounds?.count ?? 0
}

// MARK: - Delegate Methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
print("Location Manager updated location.")
if let location = manager.location {
self.locationUpdateWithNewLocation(location)
}
}

func locationUpdateWithNewLocation(location: CLLocation)
{
GroundHelper.getAllGroundLocations(location, completionHandler: { (grounds: [Ground]?) -> Void in
self.grounds = grounds?.sort({$0.distance < $1.distance})
})

}
}

最佳答案

看起来这是因为 GroundHelper.getAllGroundLocations 在后台队列中返回值。尝试将其分派(dispatch)到主队列中:

 GroundHelper.getAllGroundLocations(location, completionHandler: { (grounds: [Ground]?) -> Void in
dispatch_async(dispatch_get_main_queue(),{
self.grounds = grounds?.sort({$0.distance < $1.distance})
})
})

关于swift - CellForRow 被延迟调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33009628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com