快速UIRefresh控件在UITableView中拖动以刷新会导致应用程序崩溃

问题描述 投票:0回答:2

我在TableView中有一个UIRefreshControll。一切正常,但是如果我拉表速度太快,应用程序将崩溃。错误:致命错误:索引超出范围

[当我从Button调用函数refreshData时,不会出现崩溃。有点奇怪,因为refreshControll与调用动作没有什么不同。我希望有人能帮助我。

private let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(self.refreshData), for: .valueChanged)

if #available(iOS 10.0, *) {
                    self.tableView.refreshControl = self.refreshControl
                } else {
                    self.tableView.addSubview(self.refreshControl)
                }

@objc private func refreshData() {

    if isRefreshing {
        self.refreshControl.endRefreshing()
        return
    }
    isRefreshing = true
    print("refresh")

    guard let url = URL(string: taskURL) else {
        return
    }

    let request = URLRequest(url: url)

    urlSessionBackground.dataTask(with: request) { (dataOpt, response, error) in

        if error != nil {
            DispatchQueue.main.async {
                let alert = UIAlertController(title: "Loading Error", message: "No Internet Connection. Please try again later.", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: {(action: UIAlertAction!) in
                    self.refreshControl.endRefreshing()
                }))
                self.present(alert, animated: true)
            }
            return
        }else {
            self.data.removeAll()
            self.userImage.removeAll()
            self.currentData.removeAll()
            self.taskDetails.removeAll()
        }

        guard let dataNew = dataOpt else {
            return
        }

        let appdata = try? JSONDecoder().decode(TaskList.self, from: dataNew)

        for data in appdata!{

              self.data.append(CellData.init(image: UIImage(named: "Profile"), message: data.name, checkImage: data.status))
                self.userImage.append("")

              self.taskDetails.append(TaskDetails.init(status: data.status, assigned: data.assigneUid, time: data.duedateTsUTC, priority: data.priority, desc: data.desc, uid: data.uid))


        }

        self.currentData = self.data

        DispatchQueue.main.async {
            self.tableView.reloadData()
            self.searchBar.text = ""
            self.isRefreshing = false
            self.refreshControl.endRefreshing()
        }

    }.resume()
}
ios swift web-services refresh
2个回答
0
投票

尝试一下

    if error != nil {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: "Loading Error", message: "No Internet Connection. Please try again later.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: {(action: UIAlertAction!) in
                self.refreshControl.endRefreshing()
            }))
            self.present(alert, animated: true)
        }
        return
    }else {
        if self.data != nil{            
           self.data.removeAll()
        }
        if self.userImage != nil{            
           self.userImage.removeAll()
        }
        if self.currentData != nil{            
           self.currentData.removeAll()
        }
        if self.taskDetails != nil{            
           self.taskDetails.removeAll()
        }
    }

0
投票

只需尝试一下

if isRefreshing {
    self.refreshControl.endRefreshing()
    self.data.removeAll()
    self.userImage.removeAll()
    self.currentData.removeAll()
    self.taskDetails.removeAll()
    self.tableView.reloadData()
    return
}
© www.soinside.com 2019 - 2024. All rights reserved.