【问题标题】:How to make Http request load on time before table view cell如何在表格视图单元格之前按时加载 Http 请求
【发布时间】:2019-01-26 23:23:14
【问题描述】:

由于我是 iOS 新手,我可能在这里遗漏了设计或简单的细节,但无法弄清楚这一点:

我有一个名为getWeatherData()func,它基于从另一个视图控制器接收到的位置,从http 请求中获取天气状况并将其分配给HikeModel。该函数在ViewDidLoad()的方法中调用。

func 获得天气状况后,会将其传递给子视图控制器。哪个子 VC 是表视图控制器。 问题是我的TableViewCell 在父视图控制器中的加载速度比 http 请求要快。当单元格标签和图像尝试检索应该分配值的信息时,它们什么都不是,因为http 请求没有按时完成。

我也尝试使用DispatchQueue.main.async,但没有显示出任何差异。

我正在使用 Swift4 执行原生 http 请求。

关于我的项目结构的更多细节,我正在做基于这项工作:https://medium.com/@phillfarrugia/re-creating-the-siri-shortcuts-drawer-interaction-9b2bc94e0b05

ViewDidLoad

let group = DispatchGroup()

group.enter()

DispatchQueue.main.sync {
    self.getWeatherData(hikeLocation: self.startHikeLocationString)
    group.leave()
}

方法发送信息fillDrawer,其中应包含分配给 HikeModel 的天气变量。

private func configureDrawerViewController() {
    let compressedHeight = ExpansionState.height(forState: .compressed, inContainer: view.bounds)
    let compressedTopConstraint = view.bounds.height - compressedHeight
    containerViewTopConstraint.constant = compressedTopConstraint
    previousContainerViewTopConstraint = containerViewTopConstraint.constant

    // NB: Handle this in a more clean and production ready fashion.
    if let drawerViewController = children.first as? DrawerViewController {
        //send distnace too
        drawerViewController.delegate = self
        drawerViewController.fillDrawer(hike: self.hikeModel, userLocation: self.userLocation) 
    }

TableView CellsForRowAt

cell.temperature.text = hikeModel.temperature
cell.weather.text = hikeModel.weather
cell.weatherIcon.text = hikeModel.weatherIcon
cell.humidity.text = hikeModel.humidity
cell.barometer.text = hikeModel.barometer
cell.sunrise.text = hikeModel.sunrise
cell.sunset.text = hikeModel.sunset

我希望子控制器中的 tableViewCell 在父视图控制器中完成 http 请求后加载。

【问题讨论】:

    标签: ios swift uitableview openweathermap


    【解决方案1】:

    您的代码不完整,无法识别问题,但您应该这样做:

    1) 然后从 web 服务加载数据,

    2) 重新加载表格视图

    在你的ViewDidLoad

    let group = DispatchGroup()
            group.enter()
    
            DispatchQueue.main.sync {
                self.getWeatherData(hikeLocation: self.startHikeLocationString)
               group.leave()
            }
    

    数据建模完成后添加行,应该在ma​​inQueue

    yourtable.reloadData()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多