func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let order = orders[indexPath.row]
guard orders.count > indexPath.row else {
print("Index out of range")
return
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! OrderDetailsController
viewController.passedValue = order.id
self.present(viewController, animated: true , completion: nil)
}
每當我關閉我的應用程序(轉到後臺)並重新打開它崩潰。Swift:致命錯誤:索引超出範圍
fatal error: Index out of range
2017-06-18 18:09:33.726310 JaeeDriver[1378:563304] fatal error: Index out of range
我不知道它爲什麼這樣做。需要注意的是,在ViewDidLoud
我有這行代碼來更新我的表
var timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: "GetOrders", userInfo: nil, repeats: true)
,每當更新情況,我有這樣的代碼在GetOrders
函數的開始
func GetOrders(){
orders = []
去除是非常重要的舊的數據,並用新的
更新替換
在GetOrder
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
號段和行的末尾:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return orders.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OrderCell", for: indexPath) as! OrderCell
let entry = orders[indexPath.row]
cell.DateLab.text = entry.date
cell.shopNameLab.text = entry.shopname
cell.shopAddLab.text = entry.shopaddress
cell.nameClientLab.text = entry.clientName
cell.clientAddLab.text = entry.ClientAddress
cell.costLab.text = entry.Cost
cell.perefTimeLab.text = entry.PerferTime
cell.Shopimage.hnk_setImage(from: URL(string: entry.Logo))
return cell
}
任何幫助,請將不勝感激。
每當'orders'數組發生變化時調用'tableView.reloadData()'。 – deadbeef
請檢查我更新的問題 – leo0019
斷點您的應用程序,您可以在此下訂單,並在此行中查看indexPath和orders.count –