swift - API to table view, Add columns and refresh -
attaching code here. api: " https://bittrex.com/api/v1.1/public/getmarketsummaries "
i wanting add "high" , "low" in columns beside "marketname" also, want refresh every 10 seconds.
the refresh part, getting error, sendupdaterequest()
.
the full code is:
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 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 }
this need. copy paste entire code.
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 nserror{ print(error) } } }.resume() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can 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 } }
Comments
Post a Comment