DidSelectRowAt 方法中的混合顺序

Mixed order in DidSelectRowAt method

我不明白为什么我的调试器首先执行方法的底部,我的意思是,它首先忽略 Api 调用,转到方法的底部,然后返回到 Api 来电。
像这样:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SecondVC") as? SecondVC

//Method for AlamoFire API call 1 here
APIManager.shared.getDetailsCity(city: indexPath.row){ (details) in
//code for the method
}

//Method2 for AlamoFire API call 2 here
APIManager.shared.getDetailsCountry(country: indexPath.row){ (details) in
//code for the method2
}
self.navigationController?.pushViewController(secondVC!, animated: true)
}

code for the methodcode for the method2 是在所有其他事情之后产生的。
此外,有时 code for the method2code for the method

之前被读取

正如 Stefan 指出的,APIManager 调用似乎异步执行闭包。

您可以执行以下操作:

  • 在第二个视图控制器中创建一个整数属性index
  • 当用户点击一个单元格时,实例化该视图控制器并在推送它之前设置 vc.index = indexPath.row

在第二个视图控制器中,在viewDidLoad:

  • 启动进度指示器
  • 向用户提供一些信息,例如“正在加载数据 - 请稍候”
  • index 值调用 API 经理

在闭包中,更新 UI(确保此代码在主线程中运行)

所有闭包返回后(可能使用调度组),隐藏进度指示器