【问题标题】:AFNetworking's setImageWithURLRequest sets image in wrong cell after scroll (iOS, Swift)AFNetworking 的 setImageWithURLRequest 在滚动后将图像设置在错误的单元格中(iOS、Swift)
【发布时间】:2014-08-29 14:05:29
【问题描述】:

我使用带有dequeueReusableCellWithIdentifierafnetworking+uiimageview 的表格。我的一些细胞有图像,有些没有。如果我在加载图像之前滚动表格,成功块会将图像放入重复使用的错误单元格中。例如图像在 #2 单元格中,但滚动后它出现在 #8 号单元格中,因为此时 #8 位于第二个位置。 setImageWithURLRequest 可以和dequeueReusableCellWithIdentifier 一起使用吗?

我的代码:

    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

抱歉,如果我的问题与另一个问题重复。我发现了很多类似的问题,但我还没有找到解决方案。我尝试添加重载

tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

进入我的成功块,但它没有帮助。

更新: 我还注意到,当我的所有单元格都有图像时,我没有这个问题。如果我理解正确,原因是:当尝试请求新图像时,AFNetworking 正在中止对同一单元格的先前请求。但如果我在单元格中没有图像,它不会中止。如何手动完成?

【问题讨论】:

    标签: ios uitableview swift uiimage afnetworking-2


    【解决方案1】:

    当单元格被表格视图重用时,图片下载仍在后台处理。完成后,cell 指向具有不同内容的重用单元格。

    你有两个选择:

    1. cell.imageView.image = nil 之后的图像视图上调用cancelImageRequestOperation
    2. 在完成处理程序中,不要引用cell;而是使用您的数据模型从表格视图中请求正确的单元格。

    【讨论】:

    • 1.正是我需要的!谢谢! 2. 你什么意思?我应该再次使用 dequeueReusableCellWithIdentifier 创建它吗?
    • 不,只要弄清楚索引路径,然后在表格视图(不是视图控制器)上调用cellForRowAtIndexPath:
    【解决方案2】:

    检查单元格是否可见

    let visibleCells = tableView.visibleCells as NSArray
    cell.imageView.setImageWithURLRequest(url_request, placeholderImage: placeholder, success: { [weak cell] (request:NSURLRequest!,response:NSHTTPURLResponse!, image:UIImage!) -> Void in
                    if let cell_for_image = cell {
                        if(visibleCells.containsObject(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()
                    }
                })
    

    【讨论】:

    • 你好。谢谢你的回答,但我认为单元格总是可见的,因为我重复使用了单元格。它总是相同的单元格对象,但具有不同的内容。不是吗?
    • 然后您必须检查该单元格中的独特内容。就像他们每个人的一些自定义标识符一样。一些财产...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多