所以,我一直试图从股票市场的API中获取数据,我希望每隔10-15秒刷新一次。 API是" https://bittrex.com/api/v1.1/public/getmarketsummaries"。 我有名字显示,但是当我必须显示" Last"这是一个小数,我无法在这里显示:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath)
let item = self.listData[indexPath.row]
cell.textLabel?.text = item["MarketName"] as? String
cell.detailTextLabel?.text = item["Last"] as? String
print(self.listData.count)
return cell
}
我希望在这个项目上获得更多帮助。 我是化学工程师,但想实现我的想法并开始自己编码。 如果您能通过我的电子邮件(siddhantmehandru@gmail.com)与我联系,我将不胜感激。 我也准备好补偿这项工作:)
BTW,这是完整的代码:
import UIKit
var listData = [[String : AnyObject]]()
class DemoJsonTableViewController: UITableViewController {
var listData = [[String : AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
let url:String = "https://bittrex.com/api/v1.1/public/getmarketsummaries"
let urlRequest = URL(string: url)
URLSession.shared.dataTask(with: urlRequest!) { (data, response, error) in
if(error != nil){
print(error.debugDescription)
}
else{
do{
var response = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
self.listData = response["result"] as! [[String:AnyObject]]
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch let error as NSError{
print(error)
}
}
}.resume()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.listData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let item = self.listData[indexPath.row]
cell.textLabel?.text = item["MarketName"] as? String
cell.detailTextLabel?.text = item["Last"] as? String
print(self.listData.count)
return cell
}
}
谢谢!
答案 0 :(得分:0)
试试这个:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath)
let item = self.listData[indexPath.row]
cell.textLabel?.text = item["MarketName"] as? String
let lastValue = item["Last"] as? NSNumber
cell.detailTextLabel?.text = lastValue?.stringValue
print(self.listData.count)
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: (#selector(self.sendUpdateRequest)), userInfo: nil, repeats: true)
}
每10秒后重复调用的功能。
func sendUpdateRequest()
{
let url:String = "https://bittrex.com/api/v1.1/public/getmarketsummaries"
let urlRequest = URL(string: url)
URLSession.shared.dataTask(with: urlRequest!) { (data, response, error) in
if(error != nil){
print(error.debugDescription)
}
else{
do{
var response = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
self.listData = response["result"] as! [[String:AnyObject]]
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch let error as NSError{
print(error)
}
}
}.resume()
}
答案 1 :(得分:0)
您可以将行替换为:
cell.detailTextLabel?.text = String(describing: item["Last"] as? Double)