【问题标题】:iOS UIImageView not refreshing automaticallyiOS UIImageView 不会自动刷新
【发布时间】:2015-11-05 05:51:16
【问题描述】:

我正在关注 this wonderful tutorial 学习 iOS 编程,但我的目标是 iOS 9 并进行一些小修改。

在下面的 tableView() 函数中,我可以下载缩略图并调用我的处理程序,这从控制台打印出的两条日志记录行可以看出。但是,当应用程序运行时(在模拟器中),我必须单击每个表格单元格才能显示图像。我试图查看UIImageView 或表格单元格中是否有refresh() 或类似的东西,但什么也没找到。

如何让图片在收到数据后立即显示?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier)!
    let album = self.albums[indexPath.row]

    // ... setting up other cell's data, see the tutorial link

    // Start by setting the cell's image to a static file
    // Without this, we will end up without an image view!
    cell.imageView?.image = UIImage(named: "Blank52")

    let request: NSURLRequest = NSURLRequest(URL: thumbnailURL)
    let urlSession = NSURLSession.sharedSession()
    urlSession.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
        print("received thumbnail \(thumbnailURLString)") // reached
        if error == nil {
            // Convert the downloaded data in to a UIImage object
            let image = UIImage(data: data!)
            // Update the cell
            dispatch_async(dispatch_get_main_queue(), {
                print("...dispatched thumbnail image for \(thumbnailURLString)") // reached
                if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                    cellToUpdate.imageView?.image = image
                }
            })
        }
    }).resume()

    return cell
}

【问题讨论】:

    标签: swift uiimageview uikit ios9


    【解决方案1】:

    这部分没有意义:

    if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
        cellToUpdate.imageView?.image = image
    

    这似乎是在创建一个无限循环。每次调用cellForRowAtIndexPath 都会导致另一个调用cellForRowAtIndexPath。

    我想你的意思是:

    cell.imageView?.image = image
    

    你已经知道这个单元格了。您刚刚在调用此块之前创建了它。您无需再次查找。

    【讨论】:

      【解决方案2】:

      我想出了一个解决办法。如果我也稍微更新一下文本,图像会直接显示,而无需单击表格视图单元格。

      cellToUpdate.imageView?.image = image
      cellToUpdate.textLabel?.text = "\(album.title) " // a space is added to the end to make a difference and force the cell to be updated
      

      对我来说,这听起来像是UITableViewCell(或模拟器?还没有在真实设备上尝试过)中的一个错误。

      【讨论】:

        【解决方案3】:

        为什么你只尝试不使用 dispatch_async(dispatch_get_main_queue()) 部分。

        dispatch_async(dispatch_get_main_queue(), {
                    print("...dispatched thumbnail image for \(thumbnailURLString)") // reached
                    if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                        cellToUpdate.imageView?.image = image
                    }
                })
        

        到

        cell.imageView?.image = image
        

        【讨论】:

          【解决方案4】:

          当您获得您尝试更新的单元格时,您可能需要转换该单元格。

          dispatch_async(dispatch_get_main_queue(), {
                  print("...dispatched thumbnail image for \(thumbnailURLString)") // reached
                  if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell {
                      cellToUpdate.imageView?.image = image
              }
          })
          

          【讨论】:

            【解决方案5】:

            Swift 5 设置好图片后,在cellForRowAt委托方法中放入如下代码即可。

                    UIView.performWithoutAnimation {
                       tableView.beginUpdates()
                       tableView.endUpdates()
                    }
                // Full Method
                    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                            guard let cell = tableView.dequeueReusableCell(withIdentifier: "PhotoCell", for: indexPath) as? PhotoCell else { return UITableViewCell() }
                            let model = self.arrPhotos?[indexPath.row]
            // Below method download image from url using asycn
                            SwiftHelper.downloadImageFrom(strUrl: model?.url ?? "") {[weak self] (img) in
                                guard let _ = self, let image = img else { return }
                                print("ImgDownloaded")
                                cell.imgView.image = image
                               // cell.layoutIfNeeded()
                                UIView.performWithoutAnimation {
                                   tableView.beginUpdates()
                                   tableView.endUpdates()
                                }
                            }
                            return cell
                        }
            

            要在图像上制作动画,您可以删除 performWithoutAnimation 块。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-11-29
              • 2018-04-20
              • 2017-05-30
              • 1970-01-01
              • 2014-02-13
              • 2019-05-25
              • 2013-04-07
              • 1970-01-01
              相关资源
              最近更新 更多