gpt4 book ai didi

swift - 表格 View 的API,添加数值和添加列,每10秒刷新一次表格

转载 作者:行者123 更新时间:2023-11-28 08:02:47 25 4
gpt4 key购买 nike

因此,我一直在尝试从股票市场的 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
}
  1. 我想从 API 添加一个显示“高”、“低”的列。
  2. 我希望它每 10-15 秒刷新一次。

我需要更多关于这个项目的帮助。我是一名化学工程师,但想实现我的想法并开始自己编码。如果您能通过我的电子邮件 (siddhantmehandru@gmail.com) 与我联系,我将不胜感激。我也准备好补偿这项工作了:)

顺便说一句,这里是完整的代码:

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
}
}

谢谢!

最佳答案

试试这个:

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()
}

关于swift - 表格 View 的API,添加数值和添加列,每10秒刷新一次表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com