【问题标题】:After scrolling table cell image display previous images in swift. Though the image url display correct滚动表格单元格图像后快速显示以前的图像。虽然图片网址显示正确
【发布时间】:2016-07-08 03:27:41
【问题描述】:

我的单元格图像有问题。我在下面描述它。
具有包含 1000 个单元格的表格的屏幕。
每个单元由 a) 图像 b) 标签组成。
图片和标签数据来自json。
通过使用 AFNetwork,我获取 json 并绑定标签和图像。问题出在图像上。标签完美结合。从 json 图像 url 也正确出现。但是在开始滚动时显示以前的单元格图像。 第一次滚动未开始时,所有图像都完美显示。但是在滚动问题开始并显示以前的单元格图像之后。

代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if self.arrPostImage![indexPath.row] as! String == ""
    {
        let cell1: CellNewNoImage = tableView.dequeueReusableCellWithIdentifier("cell2") as! CellNewNoImage
        return cell1
     }
     else
     {
         let cell1: CellForMyActivityMainTableView = tableView.dequeueReusableCellWithIdentifier("cell") as! CellForMyActivityMainTableView
        cell1.title.text = self.arrTitle![indexPath.row] as? String
        cell1.postDate.text = self.arrPostDate![indexPath.row] as? String
        cell1.postDescription.text = self.arrPostDescription![indexPath.row] as? String

        //=========== Problem  Start  ====================        
        self.arrPostPersonImg![indexPath.row] =  self.arrPostPersonImg![indexPath.row].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
        let imgPath = self.arrPostPersonImg![indexPath.row]
        let url12 = NSURL(string:imgPath as! String )
        getDataFromUrl(url12!) { (data, response, error)  in
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            guard let data = data where error == nil else { return }
            cell1.postPersonImg.image = UIImage(data: data)
        }
    }
    self.arrPostImage![indexPath.row] =  self.arrPostImage![indexPath.row].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
    let imgPath2 = self.arrPostImage![indexPath.row]
    let url122 = NSURL(string:imgPath2 as! String )
    if let url = NSURL(string:imgPath2 as! String) {
    if let data = NSData(contentsOfURL: url) {
        cell1.postImage.image = UIImage(data: data)
    }        
}
return cell1
}

}

func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void))
{
    NSURLSession.sharedSession().dataTaskWithURL(url)
    { (data, response, error) in
        completion(data: data, response: response, error: error)
    }.resume()
}

【问题讨论】:

标签: ios swift


【解决方案1】:

当您使用 URL 设置 postPersonImage 时 在您的图像路径之前设置该图像视图图像 nil 并尝试。喜欢

cell1.postPersonImg.image = nil

【讨论】:

    【解决方案2】:

    在加载新图像之前,您应该从单元格中删除之前的图像。

    我实际上做了一个 Pod 来管理图像的缓存,所以你不会多次下载同一个图像:https://cocoapods.org/?q=cmimageloader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      相关资源
      最近更新 更多