AFNetworking的setImageWithURLRequest在滚动后将图像设置在错误的单元格中(iOS,Swift) [英] AFNetworking's setImageWithURLRequest sets image in wrong cell after scroll (iOS, Swift)
问题描述
我将表与 dequeueReusableCellWithIdentifier
和 afnetworking + uiimageview
一起使用。我的某些单元格有图片,有些则没有。如果在加载图像之前滚动表,则成功块会将图像放置在重用的错误单元格中。例如,图像在#2单元格中,但是在滚动之后它出现在#8单元格中,因为#8在那一刻位于第二位置。是否可以将 setImageWithURLRequest
与 dequeueReusableCellWithIdentifier
一起使用?
I use table with dequeueReusableCellWithIdentifier
and afnetworking+uiimageview
. Some of my cells have images, some haven't. If I scroll my table before an image has loaded, success block will put image in reused wrong cell. For example image was in cell #2, but after scroll it appears in cell number #8 because #8 was on second position in that moment. Is it possible to use setImageWithURLRequest
with dequeueReusableCellWithIdentifier
together?
我的代码:
let cell = tableView.dequeueReusableCellWithIdentifier("simpleCell", forIndexPath: indexPath) as UITableViewCell!
cell.textLabel.text = fields[indexPath.row]["name"] as String
cell.imageView.image = nil
if let image = fields[indexPath.row]["image"] as? String {
if (image != "") {
let image_url = NSURL(string: image)
let url_request = NSURLRequest(URL: image_url)
let placeholder = UIImage(named: "no_photo")
cell.imageView.setImageWithURLRequest(url_request, placeholderImage: placeholder, success: { [weak cell] (request:NSURLRequest!,response:NSHTTPURLResponse!, image:UIImage!) -> Void in
if let cell_for_image = cell {
cell_for_image.imageView.image = image
cell_for_image.setNeedsLayout()
}
}, failure: { [weak cell]
(request:NSURLRequest!,response:NSHTTPURLResponse!, error:NSError!) -> Void in
if let cell_for_image = cell {
cell_for_image.imageView.image = nil
cell_for_image.setNeedsLayout()
}
})
}
}
return cell
对不起,如果我的问题重复了另一个。我发现了很多类似的问题,但没有找到解决方法。我尝试添加重新加载
sorry if my question duplicates another. I found a lot of similar questions, but I haven't found solution. I tried to add reload
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
进入我的成功块,但这无济于事。
into my success block, but it doesn't help.
更新:
我也注意到,在所有单元格都有图像的情况下,我没有这个问题。如果我正确理解原因,则是:当尝试请求新图像时,AFNetworking正在中止对同一单元的先前请求。但是,如果我没有在单元格中成像,它不会中止。我该如何手动?
UPDATE: I also noticed, that I have not this problem in cases when all my cells has an images. If I understood correctly the reason is: AFNetworking is aborting previous request for the same cell, when try to request new image. But If I haven't image in cell it will not abort. How can I do it manually?
推荐答案
当表格视图重用该单元格时,图像下载仍在背景。完成后,单元格
指向具有不同内容的重用单元格。
When the cell is reused by the table view, the image download is still processing in the background. When it completes, cell
points to a reused cell with different content.
您有两个选择:
- 在
cell.imageView.image之后在图像视图上调用
cancelImageRequestOperation
= nil - 在完成处理程序中,不要引用
cell
;而是使用数据模型从表视图中请求正确的单元格。
- Call
cancelImageRequestOperation
on the image view aftercell.imageView.image = nil
- In the completion handler, don't refer to
cell
; instead use your data model to request the correct cell from the table view.
这篇关于AFNetworking的setImageWithURLRequest在滚动后将图像设置在错误的单元格中(iOS,Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!