2017-02-21 53 views
0

我有一个通过this third party library制作的RSS源。滚动浏览tableview时遇到问题。每当我向下滚动标签文本更改但一段时间后图像更改。在短短的几秒钟内,我可以看到该行的前一张图像。我希望看到一个默认图像,如果它在后端加载。用于他们的演示得到这个懒加载停止在tableview中同步加载图像

代码是:

func loadImageSynchronouslyFromURLString(_ urlString: String) -> UIImage? { 
    if let url = URL(string: urlString) { 
     let request = NSMutableURLRequest(url: url) 
     request.timeoutInterval = 30.0 
     var response: URLResponse? 
     let error: NSErrorPointer? = nil 
     var data: Data? 
     do { 
      data = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response) 
     } catch let error1 as NSError { 
      error??.pointee = error1 
      data = nil 
     } 
     if (data != nil) { 
      return UIImage(data: data!) 
     } 
    } 
    return nil 
} 

下面是从哪里该函数获取调用代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "FeedItemCell", for: indexPath) as UITableViewCell 
     let item = entries![(indexPath as NSIndexPath).row] 
if let imageView = cell.viewWithTag(1) as? UIImageView { 
      if item.mainImage != nil { 
       imageView.image = item.mainImage 
      } else { 
       if item.imageURLsFromDescription == nil || item.imageURLsFromDescription?.count == 0 { 
        item.mainImage = UIImage(named: "roundedDefaultFeed") 
        imageView.image = item.mainImage 
       } 
       DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: {() -> Void in 
        for imageURLString in item.imageURLsFromDescription! { 
         if let image = self.loadImageSynchronouslyFromURLString(imageURLString) { 
          item.mainImage = image 
          DispatchQueue.main.async(execute: {() -> Void in 
           imageView.image = image 
           self.tableView.reloadRows(at: [indexPath], with: .automatic) 
          }) 
          break; 
         } 
        } 
       }) 
      } 
     } 
    return cell 
} 

请告诉我如何显示空白在滚动后加载图片。

+0

使用'asyn'加载数据。在您的'imageView'中显示默认图像,直到您从服务器接收到数据。 –

+0

使用[SDWebImage](https://github.com/rs/SDWebImage)第三方课程。 – Ronak

+0

@the_dahiya_boy你可以举个例子吗? – Dia

回答

3

UITableViewCell s被重复使用以通过避免单元实例化来提高性能。这就是为什么你看到“最后”的图像,直到下载完成,你的“新”图像被设置。

UITableViewCell有一个称为prepareForReuse()的方法,它在重用单元之前调用。

要解决您的问题:

子类UITableViewCell,并覆盖prepareForReuse()和(重新)与占位符图像设置图像。

或:

坐落在cellForRowAt:indexPath您的图像占位符:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "FeedItemCell", for: indexPath) as UITableViewCell 
    let item = entries![(indexPath as NSIndexPath).row] 
    if let imageView = cell.viewWithTag(1) as? UIImageView { 

     imageView = myPlaceholderImage // set the placeholder you want to see while downloading the image 

     if item.mainImage != nil { 
      ... 
      // your original code 
     } 

    return cell 
} 
+0

请检查编辑问题 – Dia

+0

我的答案应该修复错误“每当我向下滚动标签文本更改但在一段时间后图像发生变化,在短短的几秒钟内,我可以看到该行的前一张图像....”) 。错误描述没有改变afaics。 'reloadRows'不应该被称为btw。 – shallowThought

+0

如何在我的视图控制器中使用prepareForReuse方法? – Dia