ios - Image url shows error while calling to display image in UITableview -
i have stored json image url in array while calling url shows error
model class
import foundation class productdetails { var productauthor: string! var productprice: int! var artimages: [artimage]! } class artimage { var imagepath: string! var imgvideotype: int! }
tableview controller storage variable
var globalarr = [productdetails]()
parsing function
func parsejsondata(data: nsdata) -> [productdetails] { var product_detail = [productdetails]() { let jsonresult = try nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers) as? nsdictionary let jsonproductdetails = jsonresult?["data"] as! [anyobject] //print("the json response is",jsonproductdetails) jsonproductdetail in jsonproductdetails{ let productdetail = productdetails() // let jsonproductimagedetails = jsonproductdetails["images"] as! [anyobject] productdetail.productauthor = jsonproductdetail["first_name"]as! string productdetail.productprice = jsonproductdetail["prodprice"]as! int // getting inside json let jsonproductimagedetails = jsonproductdetail["images"] as! [anyobject] var artimagesmodelarray = [artimage]() image in jsonproductimagedetails { let artimage = artimage(); artimage.imagepath = image["imagepath"] as! string artimage.imgvideotype = image["imgvideotype"] as! int artimagesmodelarray.append(artimage) } productdetail.artimages = artimagesmodelarray; product_detail.append(productdetail) } } catch { print (error) } return product_detail }
tableview datasource
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cellidentifier = "cell" let cell = tableview.dequeuereusablecellwithidentifier(cellidentifier, forindexpath: indexpath) as! userhomescreentableviewcell // configure cell... cell.artsauthorname.text = globalarr[indexpath.row].productauthor cell.pricelabel.text = "\(globalarr[indexpath.row].productprice)" let productdetailsobject = globalarr[indexpath.row].artimages print("@@@@@@@@@@@@@@@@@",productdetailsobject) dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), { if let url = (nsurl(string: self.globalarr[indexpath.row])) { //the error comes here....in self.global array if let data = nsdata(contentsofurl: url) { if let image = uiimage(data: data) { dispatch_async(dispatch_get_main_queue()) { () -> void in cell.artimageview.image = image } } } } }) return cell }
here have stored parse json , display details in tableview cell upto here works fine . call async way load images array shows error
any suggestion ..plz.. thank you
you need change way how you're taking image url productdetails. guess need use this:
if let url = nsurl(string: self.globalarr[indexpath.row].artimages[imageindex].imagepath) { // code }
because when perform
if let url = (nsurl(string: self.globalarr[indexpath.row]))
you object of productdetails, not image url.
Comments
Post a Comment