ios: All data is loading from json web request except SliderCollectionViewCell -


i have slidercollectionviewcontroller in uicollectionviewcell, try loading data json web, data loading without image. here load images in slidecollectionviewcell created in collectionviewcell.

import uikit  import foundation   **descriptionobject**  `class description: nsobject {      var id: int?     var product_id: int?      var mydescription: string?    var product_description: string?      var all_images: [string]?   }  **descriptioncollectionviewcontroller slidecollectionviewcontroller**  class descriptioncollectionview: uicollectionviewcontroller, uicollectionviewdelegateflowlayout{        var arrdescription = [description]()     **json request**      func loaddescription(){          activityindicator.customactivityindicatory(self.view, startanimate: true)           let url = url(string: ".........")          urlsession.shared.datatask(with:url!) { (urlcontent, response, error) in             if error != nil {                 print(error ?? 0)             }             else {                    {                     let json = try jsonserialization.jsonobject(with: urlcontent!) as! [string:any]                         let myproducts = json["products"] as? [string: any]                      let mydata = myproducts?["data"] as? [[string:any]]                        mydata?.foreach { dt in                          let oproduct = description()                         oproduct.id = dt["id"] as? int                         oproduct.product_id = dt["product_id"] as? int                           oproduct.mydescription = dt["description"] as? string                         oproduct.product_description = dt["product_description"] as? string                          if let allimages = dt["all_images"] as? [[string:any]] {                             oproduct.all_images = allimages.flatmap { $0["image"] as? string }                         }                            self.arrdescription.append(oproduct)                     }                   } catch let error nserror {                     print(error)                 }             }                dispatchqueue.main.async(execute: {                 activityindicator.customactivityindicatory(self.view, startanimate: false)                 self.collectionview?.reloaddata()             })                }.resume()     }        fileprivate let cellid = "cellid"     fileprivate let descriptioncellid = "descriptioncellid"      override func viewdidload() {         super.viewdidload()          self.loaddescription()            collectionview?.register(descriptioncell.self, forcellwithreuseidentifier: descriptioncellid)     }        override func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return arrdescription.count     }       override func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {          let cell = collectionview.dequeuereusablecell(withreuseidentifier: descriptioncellid, for: indexpath) as! descriptioncell          cell.descriptionob = arrdescription[indexpath.item]          return cell     }      **descriptioncollectionviewcell**  class descriptioncell: uicollectionviewcell, uicollectionviewdatasource, uicollectionviewdelegate, uicollectionviewdelegateflowlayout {      var descriptionob: description!{         didset{              descriptiontextview.text = descriptionob?.mydescription              coupontextview.text = descriptionob?.product_description              slidecollectionview.reloaddata()         }     }      override init(frame: cgrect) {         super.init(frame: frame)            setupcell()       }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     }         let descriptiontextview: uitextview = {           let textview = uitextview()          textview.text = "description pattern of development "           return textview     }()        let coupontextview: uitextview = {          let textview = uitextview()          textview.text = "description pattern of development "          return textview     }()        fileprivate let cellid = "cellid"      lazy var slidecollectionview: uicollectionview = {         let layout = uicollectionviewflowlayout()         layout.scrolldirection = .horizontal         let cv = uicollectionview(frame: .zero, collectionviewlayout: layout)         cv.backgroundcolor = uicolor.clear         return cv     }()        func setupcell() {            slidecollectionview.datasource = self         slidecollectionview.delegate = self          slidecollectionview.ispagingenabled = true             slidecollectionview.register(slidecell.self, forcellwithreuseidentifier: cellid)          addsubview(slidecollectionview)             addsubview(descriptiontextview)           addsubview(coupontextview)              }         func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         if let count = descriptionob?.all_images?.count{             return count         }         return 0     }      func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecell(withreuseidentifier: cellid, for: indexpath) as! slidecell           if let imagename = descriptionob?.all_images?[indexpath.item] {             cell.imageview.image = uiimage(named: imagename)         }          return cell     }   }   **slidecollectionviewcell**  class slidecell: uicollectionviewcell{         override init(frame: cgrect) {         super.init(frame: frame)            setupcellslider()       }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     }      let imageview: customimageview = {         let iv = customimageview()         iv.contentmode = .scaleaspectfill         iv.image = uiimage(named: "defaultimage3")         iv.backgroundcolor = uicolor.green         return iv     }()         func setupcellslider() {           backgroundcolor = .green            addsubview(imageview)       }    }`  **image extension**  let imagecache = nscache<anyobject, anyobject>()  class customimageview: uiimageview {      var imageurlstring: string?      func loadimageusingurlstring(_ urlstring: string) {          imageurlstring = urlstring          guard let urlencoded = urlstring.addingpercentencoding(withallowedcharacters: .urlqueryallowed) else {             print("encoding not done")             return         }          let url = url(string: urlencoded)          image = nil          if let imagefromcache = imagecache.object(forkey: urlstring anyobject) as? uiimage {             self.image = imagefromcache             return         }           if let url = url {             urlsession.shared.datatask(with: url, completionhandler: {(mydata, respones, error) in                  if error != nil {                     print(error ?? 0)                     return                 }                  if let mydata = mydata {                     dispatchqueue.main.async(execute: {                          let imagetocache = uiimage(data: mydata)                          if self.imageurlstring == urlstring {                             self.image = imagetocache                         }                          if let imagetocache = imagetocache {                             imagecache.setobject(imagetocache, forkey: urlstring anyobject)                         }                     })                 }              }).resume()         }     }  } 

json web data

enter image description here

you should use method in uiimageview subclass customimageview

so instead of

cell.imageview.image = uiimage(named: imagename) 

try this:

cell.imageview.loadimageusingurlstring(imagename) 

in cellforitem method of descriptioncell


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -