指数超出范围Swift

时间:2016-09-21 12:07:14

标签: swift tableview

我偶尔会收到错误'索引超出范围'来自以下代码:

我怀疑它与表数据重新加载的方式有关,并尝试将其移入和移出循环但没有用。

任何帮助解决此问题都将非常感谢!

override func viewDidAppear(_ animated: Bool) {


        self.tableView.rowHeight = 150

        self.tableData.removeAll(keepingCapacity: true)
        self.tableImages.removeAll(keepingCapacity: true)
        self.tableDesc.removeAll(keepingCapacity: true)

        // Register custom cell
        let nib = UINib(nibName: "vwTblCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "cell")


            let url = NSURL(string: "https://www.checkmyweave.co.uk/backend/offerstest.php")!

            let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in

                if let urlContent = data {

                    do {

                        if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] {


                            var i = 0

                            while i < jsonResult.count {


                                self.tableData.append(jsonResult[i]["offertitle"]! as! String)
                                self.tableImages.append(jsonResult[i]["imagename"]! as! String)
                                self.tableDesc.append(jsonResult[i]["offerdesc"]! as! String)


                                i = i + 1

                            }

                        }

                    } catch {

                        print("JSON serialization failed")

                    }

                } else {

                    print("ERROR FOUND HERE")
                }

                DispatchQueue.main.async(execute: { () -> Void in

                    self.tableView.reloadData()

                })
            }

            task.resume()



    }

0 个答案:

没有答案