ios - Assigning UIImage to Collection View Cell -
i have collectionview inside tableviewcontroller. collection view cells there displaying photos of users. however, fetching database url path.
i planning manipulate more on images data, chose store them in array profileimages
(but not sure if should use array of nsurl)..
(i using firebase backend lively listens changes, that's why setup isindexvalid
check part)
class tableviewcontroller: uitableviewcontroller, uicollectionviewdatasource { @iboutlet weak var collectionview: uicollectionview! var profileimages = [nsurl]() // database checks hits `retrievephotos()` func retrieveuserphotos(user: anyobject) { if let profilepiclink = user["firstimage"]! { let firstimagepath = profilepiclink as! string if let fileurl = nsurl(string: firstimagepath) { print(fileurl) let isindexvalid = profileimages.indices.contains(0) if (!isindexvalid) { profileimages.append(fileurl) print(profileimages) } else { profileimages[0] = fileurl } } } } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 1 }
until everyhing works nice. fetching image url now.
but @ point, got confused on assign image uiimage in cell. (i tried using heneke seemed easier).
func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = self.collectionview.dequeuereusablecellwithreuseidentifier("collectionviewcell", forindexpath: indexpath) as! collectionviewcell // below returns fatal error `index out of range` cell.currentimage.hnk_setimagefromurl(profileimages[indexpath.row]) // tried doesn't update uiimage. // guess it's because it's loaded once without download of images link completed: if profileimages.count > 0 { cell.currentimage.hnk_setimagefromurl(profileimages[indexpath.row]) } return cell }
what proper way of handling fetching images , assigning collection view cell's uiimage?
where call reloaditematindexpaths
depends on how you're solving async operations now. async operation should called every image downloading, can update corresponding row whenever image row has been fetched. in callback or whatever operation, call
collectionview.reloaditemsatindexpaths(myarrayofindexpaths)
myarrayofindexpaths
contain 1 item, indexpath current row. so, have async fetch method call current indexpath parameter. add empty myarrayofindexpaths
, , call reload.
Comments
Post a Comment