【问题标题】:Failure in loading image to cell in swift快速将图像加载到单元格失败
【发布时间】:2015-07-22 10:36:30
【问题描述】:

我刚刚开始使用 swift。我正在使用块和NSOperationQueuetableViewCell 中下载图像,并在完成处理程序中返回下载的图像。我正在尝试如下更新单元格。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("itemCell", forIndexPath: indexPath) as! UITableViewCell

        var itemImage = cell.viewWithTag(1000) as! UIImageView
        var itemName = cell.viewWithTag(1001) as! UILabel

        if let item = self.itemArray?[indexPath.row] {
            itemImage.image = UIImage(named: "Placeholder.jpg")
            getImageForItem(item, withCompletion: { (image) -> () in
                if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                    var imageViewToUpdate = cellToUpdate.viewWithTag(1000) as! UIImageView
                    imageViewToUpdate.image = image
                }
            })

            itemName.text = item.itemName
            }
        return cell
    }

func getImageForItem(item: item, withCompletion completion:((image: UIImage) -> ())) {
        if let image = self.imageCache.objectForKey(item.itemID) as? UIImage {
            completion(image: image)
        } else {
            let request = item.getItemImage(ItemImageSize(rawValue: 2)!, withWidth: 100, shouldFetch: false, block: { (image, tempID) -> Void in
                if image != nil {
                    self.imageCache.setObject(image, forKey: item.itemID)

                    if item.itemID == tempID {
                        completion(image: image)
                    }
                }
            })
            if request != nil {
                imageQueue.addOperation(request)
            }
        }
    }

我面临的问题是,我在 cellForRowAtIndexPath() 的完成块中成功获取了图像,但是,我无法更新单元格。对于上面的代码,下载的图像应用于 tableView 中的所有可见单元格,但是,当我向下滚动时,我只看到占位符图像。即使我在回滚时将加载的图像松动为占位符图像。

调试时发现

if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                    var imageViewToUpdate = cellToUpdate.viewWithTag(1000) as! UIImageView
                    imageViewToUpdate.image = image
                }

循环仅在第一次调用可见单元格。但没有在滚动时再次调用。我错过了什么?

【问题讨论】:

  • 当您需要所有单元格中的相同图像时,您应该将其下载到viewDidLoad并将其存储在控制器中。
  • 我没有对所有单元格使用相同的图像
  • 在 wwdc 会话 231 Cocoa Touch 最佳实践 他们鼓励避免使用 setTag:viewWithTag: 实例变量和属性提供更好的选择 我也是建议使用UIImageView+AFNetworking.h进行图片下载,只需拨打- (void)setImageWithURL:(NSURL *)url placeholderImage:(nullable UIImage *)placeholderImage;即可轻松下载

标签: ios iphone swift uitableview ios8


【解决方案1】:

我自己整理好了。我又添加了一个参数 indexPath 来跟踪它。

getImageForItem(item, indexPath: indexPath, withCompletion: { (image, imageIndexPath) -> () in
                if Set<NSIndexPath>(tableView.indexPathsForVisibleRows() as! [NSIndexPath]).contains(imageIndexPath) {
                    itemImage.image=image
                }
            })

这给了我完美的解决方案

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 2023-04-03
    • 2021-06-05
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多