滚动我的应用程序中的UIViewCollectionCells列

滚动我的应用程序中的UIViewCollectionCells列

本文介绍了当用户滚动我的应用程序中的UIViewCollectionCells列表时,行为异常(每个单元格中的数据随机变化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我使用的是UICollectionView,我决定按照以下代码使用它:

In my app I'm using UICollectionView and I've decided to use it as in the code below:

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提取的每个对象,我都会对其进行解析,并将用户的数据分配给该单元格.

So I have a cell with a UILabel and UIImage and for each object fetched from json I parse it and I assign user's data to that cell.

当我加载窗口时,我会看到带有其照片的用户名,但是当我开始滚动浏览集合视图时,用户的照片会发生变化,并且用户获得的照片应与他们应有的照片不同.

When I load the window I see usernames with photos of them, but when I start scrolling through the collection view the photos of users change and users get different photos than they should have.

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

I've read it might be something related to cell reusing (so far I know nothing about it), I just assumed it should be loaded once (when user opens this panel) and then left as it is. However it seems like this data is "fetched" each time user scrolls that list.

那么我该如何正确解决我的问题,并确保每次用户滚动列表时-数据都是正确的?

So how can I exactly fix my problem and be sure each time user scrolls the list - the data will be correct?

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

One more thing that might be useful here - method getDataFromUrl that fetches photos from urls:

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属性,将其命名为"image",然后您可以说:

What Eugene said is correct, but if you don't want to change the code you have already too much. You can add an optional UIImage property on SingleUser, call it "image," then you can say something like:

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!
  }
}

这篇关于当用户滚动我的应用程序中的UIViewCollectionCells列表时,行为异常(每个单元格中的数据随机变化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:39