ios - 当用户在我的应用程序中滚动 UIViewCollectionCells 列表时出现奇怪的行为(每个单元格中的数据随机变化)

标签 ios swift uicollectionview uicollectionviewcell uicollectionreusableview

在我的应用程序中,我使用 UICollectionView 并且我决定按照下面的代码使用它:

class UserList: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var tview: UICollectionView!
let reuseIdentifier = "cell" 
var items = NSMutableArray()

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = tview.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell

    let user:SingleUser =  self.items[indexPath.item] as! SingleUser   

    cell.username.text = user.name


        if let checkedUrl = NSURL(string: user.photo) {
            cell.userImg.contentMode = .ScaleAspectFit

            getDataFromUrl(checkedUrl) { (data, response, error)  in
                dispatch_async(dispatch_get_main_queue()) { () -> Void in
                    guard let data = data where error == nil else { return }
                    print(response?.suggestedFilename ?? "")
                    cell.userImg.image = UIImage(data: data)
                }
            }

        }

    return cell
}

所以我有一个带有 UILabelUIImage 的单元格,对于从 json 中获取的每个对象,我解析它并将用户的数据分配给该单元格。

当我加载窗口时,我看到用户名和他们的照片,但是当我开始滚动浏览 Collection View 时,用户的照片发生变化,用户得到的照片与他们应该拥有的不同。

我读过它可能与单元格重用有关(到目前为止我对此一无所知),我只是假设它应该加载一次(当用户打开此面板时)然后保持原样。然而,似乎每次用户滚动该列表时都会“获取”此数据。

那么我怎样才能准确解决我的问题并确保每次用户滚动列表时数据都是正确的?

这里可能有用的另一件事 - 从 url 获取照片的方法 getDataFromUrl:

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()
}

最佳答案

Eugene 所说的是正确的,但是如果您不想更改代码,那么您已经太多了。您可以在 SingleUser 上添加一个可选的 UIImage 属性,将其称为“图像”,然后您可以这样说:

if user.image == nil{
  getDataFromUrl(checkedUrl) { (data, response, error)  in
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
     guard let data = data where error == nil else { return }
     print(response?.suggestedFilename ?? "")
     let image  = UIImage(data: data)
     user.image = image
     //Check if this cell still has the same indexPath, else it has been dequeued, and image shouldn't be updated
     if collectionView.indexPathForCell(cell) == indexPath
     {
       cell.userImg.image = image
     }
    }
   }
  }else{
    cell.userImg.image = user.image!
  }
}

关于ios - 当用户在我的应用程序中滚动 UIViewCollectionCells 列表时出现奇怪的行为(每个单元格中的数据随机变化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510137/

相关文章:

swift - Collection View 插入项目奇怪的动画

ios - Collection View 和 TableView 方法的区别

swift - 为什么我不能将 UICollectionViewCell 向下转换为它的子类?

ios - dvc 无法在新版 iOS 的 textview 中传递 NSString 数据?

ios - 尝试对地址进行地理编码时出现异常

ios - 确定应用加载缓慢的原因

ios - 添加到 Apple Wallet 时,您可以控制通行证的顺序吗?

iphone - SceneKit – AVPlayer Material 崩溃

ios - Swift 中的 Material "pull to refresh"?

ios - 更改 UICollectionview 单元格的 XY 位置 - swift 3