ios - Why do I get nil in my Collection View Cell? -
i using alamofire in case when debug alamofire code not execute , tried manually creating url , setting headers , calling api url.
my collection view class follows.
// // categoryviewcontroller.swift // irate // // created ammar khan on 16/08/2017. // copyright © 2017 theistudio. rights reserved. // import uikit import alamofire import swiftyjson import sdwebimage class categoryviewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate { @iboutlet var mycollectionview: uicollectionview! var catarr:[string] = [] var catimage:[string] = [] let categoriesurl = "http://127.0.0.1:8000/api/places/categories" override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func viewdidappear(_ animated: bool) { loadcatlist() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 2 } func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for:indexpath) as! categorycell print("reached on here \(self.catarr.count)") cell.categoryname.text = self.catarr[indexpath.row] return cell } func loadcatlist(){ let networksession = urlsession.shared var request = urlrequest(url: url(string: self.categoriesurl )!) request.setvalue("application/json", forhttpheaderfield: "content-type") let datatask = networksession.datatask(with: request) { (data, response, error) in print("data downloaded") let jsonreadable = nsstring(data: data!, encoding: string.encoding.utf8.rawvalue) print(jsonreadable!) do{ let json = json(jsonreadable!) (_,json):(string, json) in json { let catname = json["category_name"].stringvalue let catimage = json["image"].stringvalue self.catarr.append(catname) self.catimage.append(catimage) } } catch{ print("we have json exception") } } // datatask.resume() print("downloading data") } func getcount(){ print(self.catarr.count) } }
i have tried various methods add data dictionaries failed , giving me nil. not sure doing wrong on here.
ps: not want retrieve , testing purpose therefore receiving 2 values , adding them dict.
final result:
as answer suggested below 1 of problems , after way receiving data , appending them arrays how wrong replacing following code inside statement made work.
let json = json(data!) print("the json data \(json)") (_,json):(string, json) in json { let catname = json["category_name"].stringvalue print("the catimage \(catname)") let catimage = json["image"].stringvalue self.catarr.append(catname) self.catimage.append(catimage) } dispatchqueue.main.async { self.mycollectionview.reloaddata() }
you receiving nil because network call async, when table displayed nil @ moment , , when data comes not reload collectionview, need add following after for-in loop of json
dispatchqueue.main.async { self.mycollectionview.reloaddata() }
update:
you should return number of items number of elements in datasource, in case self.catarr, modify code follows:
func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return self.catarr.count }
Comments
Post a Comment