i have tableview populated information json array. when click on cell, segues detailsviewcontroller should display
- a label displays text of cell clicked on (in case title, or entry name)
- a label shows time corresponding cell's title
- a label shows id corresponding cell's title. information (ex. time, id) come json array.
i've got #1 working, #2 need on (and i'm sure after figure out can id label easily). i'm using didselectrowatindexpath updating time label , program runs fine time label isn't updated info. label doesn't show up.
here code in tableviewcontroller file:
class earthtableviewcontroller: uitableviewcontroller { var info = [appmodel]() func getearthquakeinfo(completion: (results : nsarray?) ->void ){ datamanager.getearthquakedatafromfilewithsuccess { (data) -> void in let json = json(data: data) if let jsonarray = json.array { appdict in jsonarray { var ids: string? = appdict["id"].stringvalue var title: string? = appdict["title"].stringvalue var time: string? = appdict["time"].stringvalue var information = appmodel(idearth: ids, title: title, time: time) self.info.append(information) completion(results: self.info) } } } } override func viewdidload() { super.viewdidload() getearthquakeinfo { (info) in self.tableview.reloaddata() } } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("reuseidentifier", forindexpath: indexpath) uitableviewcell let infoarray = self.info cell.textlabel!.text = info[indexpath.row].title return cell } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) { if segue.identifier == "segue" { let vc = segue.destinationviewcontroller detailsviewcontroller let cell = (sender uitableviewcell) let title = cell.textlabel!.text vc.titledata = title } } override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let time = info[indexpath.row].time let destinationvc = detailsviewcontroller() destinationvc.timedata = time }
my detailsviewcontroller file:
class detailsviewcontroller: uiviewcontroller { @iboutlet weak var titlelabel: uilabel! @iboutlet weak var idlabel: uilabel! @iboutlet weak var timelabel: uilabel! var titledata: string! var iddata: string! var timedata: string! override func viewdidload() { super.viewdidload() let earthinfo = earthtableviewcontroller() titlelabel.text = titledata idlabel.text = iddata timelabel.text = timedata } override func didreceivememorywarning() { super.didreceivememorywarning() } }
and in case here appmodel.swift file used:
class appmodel: nsobject, printable { let idearth: string let title: string let time: string override var description: string { return "id: \(idearth), title: \(title), time: \(time), \n" } init(idearth: string?, title: string?, time: string?) { self.idearth = idearth ?? "" self.title = title ?? "" self.time = time ?? "" } }
any appreciated.
update answer: using people's comments down below able figure out answer own question. turns out, didn't need use didselectrowatindexpath. reason why added in first place because when tried let time = info[indexpath.row].time
in prepareforsegue didn't work since didn't add code needed me that. labels displaying info properly.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) { if segue.identifier == "segue" { let vc = segue.destinationviewcontroller detailsviewcontroller let cell = (sender uitableviewcell) let title = cell.textlabel!.text vc.titledata = title if let indexpath = self.tableview.indexpathforselectedrow(){ let time = info[indexpath.row].time println("\(time)") vc.timedata = time let id = info[indexpath.row].idearth vc.iddata = id } } }
this seems simple mistake regarding how you're passing data between 2 view controllers.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) { if segue.identifier == "segue" { let vc = segue.destinationviewcontroller detailsviewcontroller let cell = (sender uitableviewcell) let title = cell.textlabel!.text vc.titledata = title } } override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let time = info[indexpath.row].time let destinationvc = detailsviewcontroller() destinationvc.timedata = time }
the issue in fact sending information uiviewcontroller isn't being presented. in didselectrowatindexpath:
you're initializing view controller , sending data correctly, specific instance never shown. fix this, have few options. can either choose continue using action segue , pass data within segue using performseguewithidentifier:
& prepareforsegue:
, or can choose utilize instantiateviewcontrollerwithidentifier:
, manually present said view controller.