Swift:致命错误:索引超出范围

时间:2022-07-21 16:38:28
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)      
}

Whenever I close my app ( go to background) and reopen it it crashes.

每当我关闭我的应用程序(转到后台)并重新打开它时,它就会崩溃。

fatal error: Index out of range

致命错误:索引超出范围

2017-06-18 18:09:33.726310 JaeeDriver[1378:563304] fatal error: Index out of range

2017-06-18 18:09:33.726310 JaeeDriver [1378:563304]致命错误:指数超出范围

I have no idea why it does this. It's important to note that in ViewDidLoudI have this line of code to update my table

我不知道为什么会这样做。重要的是要注意在ViewDidLoudI中有这行代码来更新我的表

    var timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: "GetOrders", userInfo: nil, repeats: true)

and whenever the update happens, I have this code in the beginning of GetOrders function

每当更新发生时,我都会在GetOrders函数的开头有这个代码

func GetOrders (){

orders = []

to remove the old data and replace it with the new one

删除旧数据并将其替换为新数据

Update

更新

at the end of GetOrder

在GetOrder的最后

            }
            DispatchQueue.main.async {
                self.tableview.reloadData()
            }

number of sections and rows :

部分和行数:

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
}

Any help please would be appreciated.

任何帮助,请将不胜感激。

1 个解决方案

#1


1  

I think you should need to add one more array say temporder . add fresh list in this after getting fresh list change array data of order with temporder and reload it. You are clearing order data and selecting list item from table view. But at that time fresh data didn't added in order array .

我想你应该再添加一个数组说temporder。在获取新的列表后,在temporder中更改订单的数组数据并重新加载,在此添加新的清单。您正在清除订单数据并从表视图中选择列表项。但那时新订的数据没有添加到订单数组中。

#1


1  

I think you should need to add one more array say temporder . add fresh list in this after getting fresh list change array data of order with temporder and reload it. You are clearing order data and selecting list item from table view. But at that time fresh data didn't added in order array .

我想你应该再添加一个数组说temporder。在获取新的列表后,在temporder中更改订单的数组数据并重新加载,在此添加新的清单。您正在清除订单数据并从表视图中选择列表项。但那时新订的数据没有添加到订单数组中。