使用致命错误刷新tableview时应用程序崩溃:索引超出范围Swift3

时间:2017-05-10 05:57:37

标签: ios swift uitableview

我有一个UIRefreshControl并创建了一个全局对象refreshControl。刷新多次或仅刷新一次时崩溃并显示错误。

我的viewDidLoad函数

override func viewDidLoad() {
    super.viewDidLoad()

    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged)
    tableView.addSubview(refreshControl)

    tableView.register(UINib(nibName: "NewsCell", bundle: nil), forCellReuseIdentifier: "CellID")
   tableView.rowHeight = 343


}

我的视图会显示功能。这里加载的数据是一个全局声明的布尔变量

override func viewWillAppear(_ animated: Bool) {

    if !dataLoaded && newsArray.isEmpty {
       let url = urls.sharedInstance.newsUrl + String(0)
       loadDatafromUrl(uri:url)

    }
}

我的刷新功能

 func refresh(){
    newsArray.removeAll()

    let url = urls.sharedInstance.newsUrl + String(10)
    loadDatafromUrl(uri: url)
}

从网络服务器下载数据的功能

func loadDatafromUrl(uri: String){

    self.refreshControl.endRefreshing()


    print(uri)
    if let url = URL(string: uri){

        let config = URLSessionConfiguration.default
        config.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        let session = URLSession(configuration: config)

        let task = session.dataTask(with: url, completionHandler: {

            (rawData,response,error) in

            if error != nil {
                print("Couldnt load data")
                print(error?.localizedDescription as Any)
                self.navigationItem.title = "Connecting..."
                self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
                self.dataLoaded = false

            } else {

                self.dataLoaded = true

                self.navigationItem.title = ""
                if let data = rawData {
                    do{
                        if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [AnyObject]{

                            DispatchQueue.main.async {

                                for index in 0...json.count-1 {

                                    if let datas = json[index] as? [String: AnyObject] {

                                        if let title = datas["title"] as? String {
                                            if title != "nil" {

                                                let newsObj = News()
                                                newsObj.title = title
                                                newsObj.body = datas["body"] as? String
                                                let photo: String? = datas["photo"] as? String
                                                if let phot = photo {
                                                    let photourl: String? = "http://qproinnovations.com/projectpreview/unaninfo/admin/uploads/" + phot
                                                    newsObj.photo = photourl
                                                    print(photourl)
                                                }
                                                newsObj.meta = datas["meta"] as? Int

                                                self.newsArray.append(newsObj)
                                            }
                                        }

                                    }

                                }
                                 self.tableView.reloadData()
                            }


                        }
                    }catch{
                        print("error")

                    }
                }
            }
        })
        task.resume()
    }

}

2 个答案:

答案 0 :(得分:0)

newsArray.removeAll()移除refresh()并在收到您的回复后从loadDatafromUrl执行此操作,然后填充您的阵列!

第二件事从您的DispatchQueue.main.async中移除webservice call。那里没有必要。如果需要,您可以在tableview重新加载DispatchQueue.main.async

答案 1 :(得分:0)

延迟重新加载表格。我也面临着这个问题。甚至.5延迟的重新加载表对我有用