dictionary - parsing JSON to display in CollectionViewCell Swift 2 -
i starting creation of job app complement jobs website having trouble parsing indeed api use fill space in place don't have jobs. can content coming through can't seem stored dictionary , utilised create cells of collection view. code view controller below , advice appreciated after umpteen tutorials , web searches... think broke :
import uikit class viewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate { @iboutlet weak var mycollectionview: uicollectionview! var jobtitle = [[string: string]]() override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. loadjobs() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { print(jobtitle.count) return jobtitle.count } // cell returned must retrieved call -dequeuereusablecellwithreuseidentifier:forindexpath: func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { //mycollectionviewcell let mycell: mycollectionviewcell = collectionview.dequeuereusablecellwithreuseidentifier("mycell", forindexpath: indexpath) as! mycollectionviewcell let jobtitlestring = self.jobtitle[indexpath.row] as? [string:string] let truejobtitle = jobtitlestring!["jobtitle"] // as! string mycell.jobtitlelabel.textcolor = uicolor.whitecolor() mycell.jobtitlelabel.text = truejobtitle return mycell } func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { print("user tapped on: \(indexpath.row)") } func loadjobs() { let starttime = nsdate.timeintervalsincereferencedate() var pageurl = "http://api.indeed.com/ads/apisearch?publisher=8134512135661512&format=json&q=java&l=austin%2c+tx&sort=date&radius=25&st=&jt=&start=&limit=10&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=mozilla/%2f4.0%28firefox%29&v=2" let myurl=nsurl(string: pageurl) let request = nsmutableurlrequest(url:myurl!) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in // if error display alert message if error != nil { var myalert = uialertcontroller(title: "alert", message: error?.localizeddescription, preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction(title: "ok", style: uialertactionstyle.default , handler: nil) myalert.addaction(okaction) self.presentviewcontroller(myalert, animated: true, completion: nil ) return } { let jsonarray = try nsjsonserialization.jsonobjectwithdata(data!, options: .allowfragments) as? nsarray if let parsejsonarray = jsonarray { self.jobtitle = parsejsonarray as! [[string:string]] dispatch_async(dispatch_get_main_queue(), {self.mycollectionview.reloaddata()}); } } catch { print("error: \(error)") } print(self.jobtitle) } task.resume() }
}
a quick question: looking @ code, it's not clear me whether have set uicollectionviewlayout object collection view. layout manager crucial element in implementation of uicollectionview - when collection view first loaded need create layout manager, passing parameters such number of sections/rows , desired cell width/height etc. layout manager builds layout - after has happened cellforitematindexpath method called start populating collection view. if don't have layout manager initialized layout collectionviewcontroller not going think there cells in collection view!
apple offers built in default layout manager called flow layout, rather easy build own. (and there important things i'm missing out here) creating class build array of frames cells, it's not complicated.
anyway, let me know whether have in fact implemented layout manager , can take there.
Comments
Post a Comment