我有一個問題,其中一張表顯示了一個很長的鑽取列表,當用戶導航離開並返回時,我想保留其當前滾動位置。避免重新加載UITableView數據
在我的視圖控制器,我有代碼,通過調用getDrillList(optimize: true)
存儲數據在一個名爲drillListArray
的屬性中,每當視圖出現時加載UITableView的數據。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
getDrillList(optimize: true)
}
下面是加載數據
private func getDrillList(optimize: Bool = false)
{
// MAKE API CALL, THE ARRAY IS POPULATED IN THE COMPLETE HANDLER
let appDelegate = UIApplication.shared.delegate as! AppDelegate
SharedNetworkConnection.apiGetDrillList(apiToken: appDelegate.apiToken, limit: (optimize ? 13 : 0), completionHandler: { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
// 403 on no token
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
SharedNetworkConnection.apiLoginWithStoredCredentials(completionHandler: { data, response, error in
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let json = try? JSONSerialization.jsonObject(with: data!, options: [])
if let dictionary = json as? [String: Any] {
if let apiToken = dictionary["token"] as? (String) {
appDelegate.apiToken = apiToken
self.getDrillList()
}
}
})
return
}
self.drillListParser = DrillListParser(jsonString: String(data: data, encoding: .utf8)!)
self.drillListArray = (self.drillListParser?.getDrillListArray())!
DispatchQueue.main.async {
self.drillTableView.reloadData()
}
if optimize {
self.getDrillList()
}
})
}
兩個問題
代碼首先,如果用戶示出了SEGUE到另一個視圖控制器,並且然後經由Back
返回,如何可以檢查如果數據已經加載以避免再次加載它?檢查數組是否爲空是否安全?
其次,有沒有什麼影響我應該注意這種方法?
爲什麼立即downvote?請幫我修改我的問題。 – paul
我沒有投票,但最有可能是由於你的問題缺乏信息。你展示了一段代碼,它本身並不意味着什麼,並希望人們能夠在不知道你在做什麼的情況下提供幫助。請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 –
把getDrillList放在ViewWillAppear中。將解決問題。 –