Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新加载表视图时从错误中获取索引_Ios_Swift_Xcode_Uitableview_Swift3 - Fatal编程技术网

Ios 重新加载表视图时从错误中获取索引

Ios 重新加载表视图时从错误中获取索引,ios,swift,xcode,uitableview,swift3,Ios,Swift,Xcode,Uitableview,Swift3,每当我关闭我的应用程序(转到后台)并重新打开它时,它就会崩溃 致命错误:索引超出范围 2017-06-18 18:09:33.726310 JaeeDriver[1378:563304]致命错误:索引超出范围 我不知道它为什么会这样。需要注意的是,在ViewDidLoud中,我有这行代码来更新我的表 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.desel

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

致命错误:索引超出范围

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

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

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)      
}
每当更新发生时,我在
GetOrders
函数的开头都有这段代码

var timer = Timer.scheduledTimer(
    timeInterval: 4, 
    target: self, 
    selector: "GetOrders", 
    userInfo: nil, 
    repeats: true
)
删除旧数据并替换为新数据

更新

GetOrder

func GetOrders (){   
    orders = []
    ...
}
节数和行数:

func GetOrders (){   
    orders = []
    ....
    ....
    DispatchQueue.main.async {
        self.tableview.reloadData()
    }
}
       

如果您有任何帮助,我们将不胜感激。

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

每当您的
订单
数组发生更改时,请调用
tableView.reloadData()
。请检查我更新的问题断开获取let order的应用程序,并查看此行中的indexPath和orders.Count>>let order=orders[indexPath.row]用你的
numberOfSections
numberofrowsinssection
方法更新你的问题。谢谢你。如果你能在这里帮助我,我将不胜感激
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
}