在此处附加代码。 这是API:“https://bittrex.com/api/v1.1/public/getmarketsummaries”
我想在“MarketName”旁边的列中添加“High”和“Low” 另外,我想每10秒刷新一次。
sendUpdateRequest()
的刷新部分,我收到错误。
完整的代码是:
import UIKit
var listData = [[String : AnyObject]]()
class DemoJsonTableViewController: UITableViewController {
var listData = [[String : AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
var timer : Timer? = nil
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: (#selector(self.sendUpdateRequest)), userInfo: nil, repeats: true)
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()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
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
let lastValue = item["Last"] as? NSNumber
cell.detailTextLabel?.text = lastValue?.stringValue
print(self.listData.count)
return cell
}
答案 0 :(得分:0)
这里的代码有几个问题:
首先,您要在sendUpdateRequest()
内定义viewDidLoad()
。将整个函数移到这些封闭的大括号之外以消除该错误。其次,您不需要在课堂外定义listData
。
然后,您可以使用现有代码进行刷新功能。
对于High and Low的列,您需要继承UITableViewCell
并添加额外的标签。
按照此处列出的步骤进行操作:How to add more than two labels to prototype cell?。
答案 1 :(得分:0)
这就是你需要的。复制粘贴整个代码。
import UIKit
class DemoJsonTableViewController: UITableViewController {
var listData = [[String : AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(DrawerCell.self, forCellReuseIdentifier: "Cell")
self.sendUpdateRequest()
Timer.scheduledTimer(timeInterval: 10, target: self, selector: (#selector(self.sendUpdateRequest)), userInfo: nil, repeats: true)
}
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()
}
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 {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DrawerCell
let item = self.listData[indexPath.row]
cell.TitleLabel.text = item["MarketName"] as? String
cell.Description.text = String(describing: item["Last"] as? NSNumber)
cell.HighLabel.text = String(describing: item["High"] as! NSNumber)
cell.LowLabel.text = String(describing: item["Low"] as! NSNumber)
return cell
}
}
class DrawerCell: UITableViewCell {
var TitleLabel: UILabel = UILabel()
var Description: UILabel = UILabel()
var HighLabel: UILabel = UILabel()
var LowLabel: UILabel = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
TitleLabel.textColor = UIColor.black
TitleLabel.font = UIFont.init(name: "AppleSDGothicNeo-Bold", size: 10)
TitleLabel.textAlignment = .left
contentView.addSubview(TitleLabel)
Description.textColor = UIColor.black
Description.font = UIFont.init(name: "AppleSDGothicNeo-Bold", size: 8)
Description.textAlignment = .left
contentView.addSubview(Description)
HighLabel.textColor = UIColor.black
HighLabel.font = UIFont.init(name: "AppleSDGothicNeo-Bold", size: 10)
HighLabel.textAlignment = .center
contentView.addSubview(HighLabel)
LowLabel.textColor = UIColor.black
LowLabel.font = UIFont.init(name: "AppleSDGothicNeo-Bold", size: 10)
LowLabel.textAlignment = .center
contentView.addSubview(LowLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
TitleLabel.frame = CGRect(x: 10, y: 0, width: self.frame.size.width - 110, height: 20)
Description.frame = CGRect(x: 10, y: 20, width: self.frame.size.width - 110, height: 20)
HighLabel.frame = CGRect(x: self.frame.size.width - 110, y: 0, width: 100, height: 20)
LowLabel.frame = CGRect(x: self.frame.size.width - 110, y: 20, width: 100, height: 20)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func prepareForReuse() {
TitleLabel.text = nil
Description.text = nil
HighLabel.text = nil
LowLabel.text = nil
}
}