remy boys remy boys - 5 months ago 60
Swift Question

hide a View from a single item in Row (collectionViewCell Item)

sometimes my ColectionViewCell's progress bar is not visible ,

i have some items at every row and below am trying to hide ProgressBar of a single item but my progress bar got hidden from all of the items of a row

func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
forIndexPath: indexPath) as! CollectionViewCell

let url = NSURL(string: "\(attachedImgUrlDict[collectionView.tag]![indexPath.item])")// its a Url
cell.imgViewOfCell.sd_setImageWithURL(url, placeholderImage: nil, options: SDWebImageOptions.CacheMemoryOnly, progress: {
(receivedSize, expectedSize) -> Void in
// Update progress here

cell.loaderView.progress = CGFloat(receivedSize)/CGFloat(expectedSize)

}) {
(image, error, _, _) -> Void in
// Reveal image here

cell.imgViewOfCell.image = self.ResizeImage( image , targetSize: CGSizeMake( cell.imgViewOfCell.frame.width , cell.imgViewOfCell.frame.height))

cell.loaderView.hidden = true // am hiding the progress bar here when the loads complete (of a single item) but seems like its hiding the progressView of all items in the row

}
return cell
}


any idea how to fix this ???

Answer

Just a short overview, So you get your answer

UICollectionView is highly optimized, and thus only keep On-screen visible rows in memory. Now, All rows Cells are cached in Pool and are reused and not regenerated. Whenever, user scrolls the UICollectionView, it adds the just-hidden rows in Pool and reuses them for next to be visible rows.

So, now, coming to your answer

When you scroll your CollectionView, collectionView datasource method gets called again for every indexPath, and it reuses the hidden cells, Thus in other cell, where you hide your progress bar, gets reused and it still shows progress bar hidden

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

SOLUTION

RESET YOUR PROGRESS BAR VISIBILITY FOR EVERY CELL On call of cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView,
                        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
                                                                         forIndexPath: indexPath) as! CollectionViewCell

        cell.loaderView.hidden =  false //RESET ITS VISIBILITY


        let url = NSURL(string: "\(attachedImgUrlDict[collectionView.tag]![indexPath.item])")// its a Url 
            cell.imgViewOfCell.sd_setImageWithURL(url, placeholderImage: nil, options: SDWebImageOptions.CacheMemoryOnly, progress: {

                //SAME CODE

            }
        return cell
    }