【发布时间】:2017-11-20 20:05:54
【问题描述】:
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)
}
每当我关闭我的应用(转到后台)并重新打开它时,它都会崩溃。
致命错误:索引超出范围
2017-06-18 18:09:33.726310 JaeeDriver[1378:563304] 致命错误:索引超出范围
我不知道为什么会这样。需要注意的是,在ViewDidLoud我有这行代码来更新我的表
var timer = Timer.scheduledTimer(
timeInterval: 4,
target: self,
selector: "GetOrders",
userInfo: nil,
repeats: true
)
每当更新发生时,我在GetOrders函数的开头都有这段代码
func GetOrders (){
orders = []
...
}
删除旧数据并用新数据替换
更新
GetOrder结尾
func GetOrders (){
orders = []
....
....
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()。 -
请检查我更新的问题
-
在您的应用程序中设置断点,让您订购并查看 indexPath 和 orders.count
-
在这一行 >>> let order = orders[indexPath.row]
-
使用
numberOfSections和numberOfRowsInSection方法更新您的问题。
标签: ios swift xcode uitableview swift3