ios - API calls through Switch Statements -
i have 2 tableviews, first table view has fixed data never changed. when user taps on specific cell example cell number 1, api 1 called , 2nd table view loaded returned data when cell number 2 tapped api 2 called 2nd table view loaded returned data.
to solve issue have tried this.
in first table view record of table cell tapped , send information 2nd table view via prepare segue:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let destination = segue.destinationviewcontroller as? biolisttableviewcontroller { let indexpath = self.tableview.indexpathforselectedrow() if let row:int = indexpath?.row { // pass cell tapped user destination.celltapped = row } } }
then within 2nd table view use switch statement checks whether cell tapped 0,1,2 , on. , based on switch case run. each switch case has different function calls different api. see below:
import uikit struct note { var name:string var job:string } struct weathersummary { var id: string } class biolisttableviewcontroller: uitableviewcontroller { var celltapped = int() @iboutlet var tableview: uitableview! private var notes = array<note>() var bioarray = nsarray(){ didset{ tableview.reloaddata() } } override func viewdidload() { super.viewdidload() switch celltapped { case 0: test() case 1: testtwo() default: println("error") } var newitem:note = note(name: "", job: "") x in bioarray { if let id = x["employeename"] as? string{ newitem.name = id } } } func test() { println("this twest") var weatherarray = [weathersummary]() var request = nsmutableurlrequest(url: nsurl(string: "myapi-link")!) var session = nsurlsession.sharedsession() request.httpmethod = "get" uiapplication.sharedapplication().networkactivityindicatorvisible = true var err: nserror? request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in println("response: \(response)") var strdata = nsstring(data: data, encoding: nsutf8stringencoding) println("body: \(strdata)") var err: nserror? var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &err) as? nsarray uiapplication.sharedapplication().networkactivityindicatorvisible = true // did jsonobjectwithdata constructor return error? if so, log error console if(err != nil) { println(err!.localizeddescription) let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: '\(jsonstr)'") dispatch_async(dispatch_get_main_queue()) { var alert = uialertcontroller(title: "alert", message: "oops! wrong details, try again", preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "ok", style: uialertactionstyle.default, handler: nil)) self.presentviewcontroller(alert, animated: true, completion: nil) } }else { uiapplication.sharedapplication().networkactivityindicatorvisible = false // jsonobjectwithdata constructor didn't return error. but, should still // check , make sure json has value using optional binding. var newweather = weathersummary(id:"") if let parsejson = json { weather in parsejson { if let id = weather["employeename"] as? string{ println(" here \(id)") newweather.id = id } } weatherarray.append(newweather) self.bioarray = parsejson } else { // woa, okay json object nil, went worng. maybe server isn't running? let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: \(jsonstr)") } } }) task.resume() } func testtwo(){ println("this test 2") } override func didreceivememorywarning() { super.didreceivememorywarning() } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { // #warning potentially incomplete method implementation. // return number of sections. return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete method implementation. // return number of rows in section. return self.bioarray.count ?? 0 } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("biocell", forindexpath: indexpath) as! uitableviewcell let weathersummary: anyobject = bioarray[indexpath.row] if let id = weathersummary["employeename"] as? string //dont know exact syntax. { cell.textlabel?.text = id } if let job = weathersummary["jobtitle"] as? string { cell.detailtextlabel?.text = job } return cell } }
my issue:
my issue when println returned data being printed , can see it. 2nd table view empty. not display data. not sure why data not being displayed on 2nd table view. can see data using println proves api returning real data.
any suggestions?
apologies mistakes. please let me know if have made mistake , fix it.
thank you.
hard answer without seeing data or storyboard. few things should check set breakpoint , make sure calling:
- didset method of bioarray
- is numberofrowsinsection hit , returning something?
- is delegate , datasource set properly?
- is cellforrowatindexpath hit , have keys named: employeename , jobtitle
please check these things first.
Comments
Post a Comment