重新加载表视图时获取索引错误 [英] Getting index out of error when reloading table view

查看: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] 致命错误:索引超出范围

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

我不知道为什么会这样.需要注意的是,在 ViewDidLoud我有这行代码来更新我的表格

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

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

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

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

func GetOrders (){   
    orders = []
    ...
}

删除旧数据并用新数据替换

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

更新

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
}

如有任何帮助,我们将不胜感激.

Any help please would be appreciated.

推荐答案

我认为您应该再添加一个数组,比如 temporder .在使用 temporder 获取新列表更改数组数据后,在此添加新列表并重新加载它.您正在清除订单数据并从表格视图中选择列表项.但当时并没有在 order array 中添加新数据.

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 .

这篇关于重新加载表视图时获取索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆