swift - use Alamofire to show image in collectionView in ios -


i use alamofire library showing image in cell in collectionview problem when scrolling && down , collectionview showing wrong image in cell

and snippet code set cell data

override func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {     let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! cardviewcell      let pos = indexpath.item % 5      var paragraphstyle = nsmutableparagraphstyle()     paragraphstyle.linespacing = 2.5     paragraphstyle.alignment = .right     paragraphstyle.linebreakmode = .bytruncatingtail      if let text = currentcollection.titles?[indexpath.item] {         var attrstring = nsmutableattributedstring(string: text)         attrstring.addattribute(nsparagraphstyleattributename, value: paragraphstyle, range:nsmakerange(0, attrstring.length))         cell.title.attributedtext = attrstring     } else {         cell.title.text = currentcollection.titles?[indexpath.item]     }     if let catid = currentcollection.catids?[indexpath.item] {         cell.iconimage.image = uiimage(named: icons[catid-1])     }      cell.title.font = uifont(name: "b yekan", size: 14)     cell.title.preferredmaxlayoutwidth = cell.frame.size.width - 48      cell.iconimage.image = cell.iconimage.image!.imagewithrenderingmode(.alwaystemplate)     cell.iconimage.tintcolor = uicolor.whitecolor()      let imageurl = currentcollection.urlimages![indexpath.item]      if let image = currentcollection.imagecache.objectforkey(imageurl) as? uiimage {         cell.backimage.image = image     } else {         cell.backimage.image = nil          cell.request = alamofire.request(.get, imageurl).validate(contenttype: ["image/*"]).responseimage() {             (request, _, image, error) in             if error == nil && image != nil {                 self.currentcollection.imagecache.setobject(image!, forkey: request.urlstring)                  cell.backimage.image = image             }         }     }       cell.layer.shadowpath = uibezierpath(roundedrect: cell.bounds, cornerradius: cell.layer.cornerradius).cgpath      if indexpath.item == currentcollection.titles!.count-3 {         currentcollection.page++         appendtitlesinpage(currentcollection.page)     }        return cell } 

can me wrong? please!

the image request takes time - during time request keeps reference cell, , sets cell's backimage.image once request complete. unfortunately, time, cell may have been scrolled off-screen , may being re-used @ indexpath, image incorrect. instead, should use table view's cellforitematindexpath method correct cell (if cell no longer visible, return nil prevent error).


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -