Zaid Pathan
Zaid Pathan

Reputation: 16820

Images are setting incorrectly in cellForRowAtIndexPath in SDWebImage

I'm using SDWebImage, I'm creating download progress effect same as WhatsApp app.

The problem is that when I scroll tableView, It is showing incorrect images for cells.(i.e: When cells are reused random images are setting in cells.)

Here is my code in Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:ImageCellTableViewCell = self.IBtblImageTable.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as ImageCellTableViewCell
    var url:NSURL = NSURL(string: imageArray[indexPath.row])!
    cell.tag = indexPath.row
    manager?.downloadImageWithURL(url, options: SDWebImageOptions.ProgressiveDownload, progress: { (recievedSize:Int, expectedSize:Int) -> Void in

        if(recievedSize>0){

            var progress:Float = 0

            progress = Float(recievedSize)/Float(expectedSize)

            println(CGFloat(progress))


        }else{}

    }, completed: { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool, url:NSURL!) -> Void in


        if ((image) != nil && cell.tag == indexPath.row)
        {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                cell.IBimgCellImage.image = image
            })

        }


    })

    return cell
}

Upvotes: 1

Views: 1027

Answers (1)

Zaid Pathan
Zaid Pathan

Reputation: 16820

Declared dictionary:

var progressDic: [String:CGFloat] = [:]

then,

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell:ImageCellTableViewCell = self.IBtblImageTable.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as ImageCellTableViewCell
var url:NSURL = NSURL(string: imageArray[indexPath.row])!
var progressCircle :CAShapeLayer?
cell.IBimgCellImage.layer.sublayers = nil
cell.IBimgCellImage.image = nil

if(progressCircle == nil && progressDic[String(indexPath.row)] != 1.0){
    progressCircle = getProgressCircleLayer(cell.IBimgCellImage.center)
    cell.IBimgCellImage.layer.addSublayer(progressCircle)
    }

manager?.downloadImageWithURL(url, options: SDWebImageOptions.ProgressiveDownload, progress: { (recievedSize:Int, expectedSize:Int) -> Void in
    var progress:Float?

    if(recievedSize>0){
        progress = Float(recievedSize)/Float(expectedSize)
       self.progressDic.updateValue(CGFloat(progress!), forKey: String(indexPath.row))
            progressCircle?.strokeEnd = self.progressDic[String(indexPath.row)]!


    }else{}

}, completed: { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool, url:NSURL!) -> Void in


   dispatch_async(dispatch_get_main_queue(), { () -> Void in

                if (tableView.indexPathForCell(cell)?.row == indexPath.row)
                {
                cell.IBimgCellImage.image = image
                }
            })

    })

return cell
}

That's it.

Upvotes: 2

Related Questions