2017-09-10 11 views
0

ここにコードを添付します。 これはAPIである:「https://bittrex.com/api/v1.1/public/getmarketsummariesAPIをテーブルビューに追加し、列を追加して更新する

私は「MarketName」 の横の欄に「高」と「低」を追加したいのです。また、私はこれを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

これは必要なものです。コード全体をコピーして貼り付けます。

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 

    } 
} 
+0

これはちょうど完璧です。 私はこのプロジェクトを完了するために切望しています。 あなたは[email protected]に私に連絡してもらえますか?私はあなたにプロジェクト全体を報いたいと思います。 もう一度ありがとう –

0

はカップル:

まず、あなたがviewDidLoad()内部sendUpdateRequest()を定義しています。これらの中括弧の外側にある関数全体を移動して、そのエラーを取り除きます。次に、クラス外にlistDataを定義する必要はありません。

次に、既存のコードをリフレッシュ機能に使用できます。

高低の列については、UITableViewCellをサブクラス化し、余分なラベルを追加する必要があります。

ここで説明する手順は、How to add more than two labels to prototype cell?に従ってください。