无法通过拉动来刷新我的JSON数据,以刷新TableView Controller中



我在通过拉动刷新的JSON数据刷新我的JSON数据时都有问题。当我启动应用程序时,它正在显示数据,但是当我拉动刷新时,它就不会刷新。我已经实施了Refresh Control来刷新数据。我可以看到拉动的轮子图标以刷新,但是它没有更新JSON数据。

这是我的代码:

struct JSONData {
    let country: String
    let indicename: String
    let currPrice: String
    let chg: String
    let perChg: String
    init?(dictionary: [String:Any]) {
        guard let country = dictionary["Country"] as? String,
            let indicename = dictionary["Indicename"] as? String,
            let currPrice = dictionary["CurrPrice"] as? String,
            let chg = dictionary["Chg"] as? String,
            let perChg = dictionary["perChg"] as? String else {
                return nil
        }
        self.country = country
        self.indicename = indicename
        self.currPrice = currPrice
        self.chg = chg
        self.perChg = perChg
    }
}
       class SouthAViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,IndicatorInfoProvider {
    var datas = [JSONData]()
        var refreshControl = UIRefreshControl()
          @IBOutlet var tableview: UITableView!
        var arrowupimage : UIImage = UIImage(named : "arrowup")!
        var arrowdownimage : UIImage = UIImage(named : "arrowdown")!
        // I haven't shared the URL of parsing for security reasons
        let url=NSURL(string:"*******************************")
        let stringurl = "***************************"

        override func viewDidLoad() {
            super.viewDidLoad()
            if #available(iOS 10.0, *) {
                tableview.refreshControl = refreshControl
            } else {
                tableview.addSubview(refreshControl)
            }
            self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
            self.refreshControl.addTarget(self, action: #selector(SouthAViewController.refresh), for: UIControlEvents.valueChanged)

            navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
            self.downloadJsonWithURL()
            tableview.delegate = self
            tableview.dataSource = self
        }
        func refresh(){
            self.downloadJsonWithURL()
        }
        // downloading JSON data to display on this class
        func downloadJsonWithURL() {
             let task = URLSession.shared.dataTask(with: URL(string: stringurl)!) { (data, response, error) in
            if error != nil {
              //  print(error?.localizedDescription)
                return
            }
            if let contdata = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String:Any] {
                if let arrJSON = contdata["data"] as? [[String:Any]] {
                    self.datas = arrJSON.flatMap(JSONData.init)
                        //Reload tableView and endRefreshing the refresh control
                        DispatchQueue.main.async {
                        self.tableview.reloadData()
                        self.refreshControl.endRefreshing()
                        }
                }
            }
        }
        task.resume()
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return datas.count
        }
        //setting data on the table view cell
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "southacell", for: indexPath) as! southacell

        cell.Indicename?.text = datas[indexPath.row].indicename
        cell.Country?.text = datas[indexPath.row].country
        cell.CurrPrice?.text = datas[indexPath.row].currPrice

            return cell
        }
    }

JSON样本:

{  "data":[  
      {  
         "Country":"China",
         "Indicename":"SZSE COMPONENT INDEX",
         "date":"2017-06-23 14:53:57",
         "zone":"GMT+8",
         "CurrPrice":"10355.3",
         "Chg":"90.07",
         "PerChg":"0.88",
         "prev_close":"10265.2"
      },
      {  
         "Country":"China",
         "Indicename":"Shanghai Composite",
         "date":"2017-06-23 14:52:54",
         "zone":"GMT+8",
         "CurrPrice":"3155.9",
         "Chg":"8.44",
         "PerChg":"0.27",
         "prev_close":"3147.45"
      }
]
}

不要使用Data(contentsOf:)URL检索数据,您需要使用的是用datatask使用URLSession,以从URL检索数据,然后您需要重新加载tableView并结束RefreshControl的动画。datatask(with:)的完成块。另外,您需要做的是创建一个自定义classstruct

的数组,而不是处理多个数组。
struct Data {
    var country: String
    var indicename: String
    var currPrice: String
    var chg: String
    var perChg: String
    init?(dictionary: [String:Any]) {
        guard let country = dictionary["Country"] as? String,
              let indicename = dictionary["Indicename"] as? String,
              let currPrice = dictionary["CurrPrice"] as? String,
              let chg = dictionary["Chg"] as? String,
              let perChg = dictionary["PerChg"] as? String else {
            return nil
        }
        self.country = country
        self.indicename = indicename
        self.currPrice = currPrice
        self.chg = chg
        self.perChg = perChg
    }
}

现在仅声明一个类型[Data]的数组,然后使用此阵列将您的tableView方法。

var datas = [Data]()

现在,只需使用此单个数组存储所有数据并在tableView中显示数据。

func refresh(){
    self.downloadJsonWithURL()
}
func downloadJsonWithURL() {
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print(error?.localizedDescription)
            return
        }
        if let contdata = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String:Any] {
            if let arrJSON = contdata["data"] as? [[String:Any]] {
                self.datas = arrJSON.flatMap(Data.init)
                //Reload tableView and endRefreshing the refresh control 
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                    self.refreshControl.endRefreshing()
                }
            }
        }
    }
    task.resume()
}
//tableView methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}
//setting data on the table view cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "southacell", for: indexPath) as! southacell

    let curpricefloat : Double = Double(datas[indexPath.row].currPrice)!
    let Chgfloat : Double = Double(datas[indexPath.row].chg)!
    let perchgfloat : Double = Double(datas[indexPath.row].perChg)!
    cell.Indicename?.text = datas[indexPath.row].indicename
    cell.Indicename.font = UIFont.boldSystemFont(ofSize: 19.0)
    cell.Country?.text = datas[indexPath.row].country
    cell.PerChg?.text = "(" + String(format: "%.2f", perchgfloat) + "%)"
    cell.CurrPrice?.text = String(format: "%.2f", curpricefloat)
    cell.Chg?.text = String(format: "%.2f", Chgfloat)
    if Float((cell.Chg?.text)!)! < 0 {
        datas[indexPath.row].chg = datas[indexPath.row].chg.replacingOccurrences(of: "-", with: "")
        datas[indexPath.row].perChg = datas[indexPath.row].perChg.replacingOccurrences(of: "-", with: "")
        cell.PerChg?.text = "(" + datas[indexPath.row].perChg + "%)"
        cell.Chg?.text = datas[indexPath.row].chg
        cell.Chg?.textColor = UIColor.red
        cell.PerChg?.textColor = UIColor.red
        cell.arrow?.image = UIImage(named : "arrowdown")
    } else
    {
        cell.Chg?.textColor = UIColor.green
        cell.PerChg?.textColor = UIColor.green
        cell.arrow?.image = UIImage(named : "arrowup")
    }
    if cell.Country!.text! == "Argentina"{
        cell.countryFlag?.image = UIImage(named : "argentina")
    }
    else if cell.Country!.text! == "Brazil"{
        cell.countryFlag?.image = UIImage(named : "brazil")
    }
    else if cell.Country!.text! == "Peru"{
        cell.countryFlag?.image = UIImage(named : "peru")
    }
    else{
        cell.countryFlag?.image = UIImage(named : "unknown")
    }
    return cell
}

尝试以下内容:

override func viewDidLoad() {
    super.viewDidLoad()
    // MARK: Refresh control
    updateData.backgroundColor = .black
    updateData.tintColor = .white
    updateData.attributedTitle = NSAttributedString(string: "Updating Tap 
List...", attributes: [NSForegroundColorAttributeName: UIColor(red: 
255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)])
    updateData.addTarget(self, action: 
#selector(ViewController.loadNewData), for: UIControlEvents.valueChanged)
    tapListTableView.addSubview(updateData)
    tapListTableView.sendSubview(toBack: updateData)
        DispatchQueue.main.async {
            self.tableview.reloadData()
        }
    }
}

最新更新