我遇到了通过pull to refresh刷新我的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"
}
]
}
答案 0 :(得分:1)
请勿使用Data(contentsOf:)
从URL
检索您需要使用URLSession
datatask
的数据,以便在URL
之后从tableView
检索数据需要在RefreshControl
的完成块内重新加载datatask(with:)
并结束class
的动画。另外,您需要创建一个自定义struct
或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
}
中显示数据。
navigationView
答案 1 :(得分:0)
尝试以下方法:
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()
}
}
}