图像在 SDWebImage 的 cellForRowAtIndexPath 中设置不正确
Images are setting incorrectly in cellForRowAtIndexPath in SDWebImage
我正在使用 SDWebImage,
我正在创建与 WhatsApp 应用程序相同的下载进度效果。
问题是当我滚动 tableView 时,它显示的单元格图像不正确。(即:当单元格被重复使用时,单元格中设置了随机图像。)
这是我在 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
}
声明的字典:
var progressDic: [String:CGFloat] = [:]
那么,
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
}
就是这样。
我正在使用 SDWebImage, 我正在创建与 WhatsApp 应用程序相同的下载进度效果。
问题是当我滚动 tableView 时,它显示的单元格图像不正确。(即:当单元格被重复使用时,单元格中设置了随机图像。)
这是我在 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
}
声明的字典:
var progressDic: [String:CGFloat] = [:]
那么,
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
}
就是这样。