ios - Why is this data not displaying in my Swift 3 table -
so i'm trying parse json data retrieved server , display nicely in table, ive followed suggestions here: uitableview example swift , managed example working.
however data im parsing table remains blank. can see im going wrong?
import uikit import swiftyjson class logsviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { let animals: [string] = ["horse", "cow", "camel", "sheep", "goat"] var arraycount:int = 0 struct item { let name : string let locktime : string let type : string } // cell reuse id (cells scroll out of view can reused) let cellreuseidentifier = "cell" var items = [item]() @iboutlet weak var textuodate: uistackview! override func viewdidload() { super.viewdidload() // register table view cell class , reuse id self.tableview.register(uitableviewcell.self, forcellreuseidentifier: cellreuseidentifier) tableview.delegate = self tableview.datasource = self // additional setup after loading view. let lockid = userdefaults.standard.value(forkey: "lockid")! // let email = userdefaults.standard.value(forkey: "email")! //get data server specific lock id let u = userdefaults.standard.value(forkey: "userip")! var request = urlrequest(url: url(string: "http://\(u):3000/logs")!) request.httpmethod = "post" let poststring = "lockid=\(lockid)" request.httpbody = poststring.data(using: .utf8) let task = urlsession.shared.datatask(with: request) { data, response, error in guard let data = data, error == nil else { print("error=\(error)") return } if let httpstatus = response as? httpurlresponse, httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response)") print(response ?? " ") } let responsestring = string(data: data, encoding: .utf8) if let data = responsestring?.data(using: string.encoding.utf8) { let resstring = json(data: data) if resstring["success"].stringvalue == "true" { self.arraycount = (resstring["message"].count) print(self.arraycount) let returnedarray = resstring["message"].arrayvalue item in returnedarray { let name = string(describing: item["name"]) let locktime = string(describing: item["locktime"]) let type = string(describing: item["type"]) self.items.append(item(name:name, locktime:locktime, type:type)) } } else if resstring["success"].stringvalue == "false" { print(resstring["success"].stringvalue) } } } task.resume() dispatchqueue.main.async{ self.tableview.reloaddata() } } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return arraycount } // create cell each table view row func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell:uitableviewcell = self.tableview.dequeuereusablecell(withidentifier: cellreuseidentifier) uitableviewcell! let item = items[indexpath.row] let celltext = "\(item.name) \(item.type) at: \(item.locktime) " cell.textlabel?.text = celltext print(celltext) return cell } func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { print("you tapped cell number \(indexpath.row).") }
data not displaying because didn't reload table view after append items array. please reload table view @ end of task closure
let task = urlsession.shared.datatask(with: request) { data, response, error in // ...... // ...... self.tableview.reloaddata() }
Comments
Post a Comment