4

Hi I have an array with 100 pictures url's taken from flickr. When I use UICollectionView I display 100 cells, only 8 cells are on the screen and when I scroll down to see next 8 they are the same like the previous and when I execute

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

and println("something") it writes in console only 8 time something but the content of array is 100

I use this:

func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
    return self.photosUrls.count
}

It makes 100 cells but the cells repeats always... Anybody who knows how to beat the Swift in new version of XCode GM seed ?

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
    let cell: ImageCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as ImageCell
    let imgUrl = photosUrls[indexPath!.row]
    let api_url = NSURL.URLWithString(imgUrl)
    let URLReq = NSURLRequest(URL: api_url)
    let NSOpQ:NSOperationQueue = NSOperationQueue()
    NSURLConnection.sendAsynchronousRequest(URLReq, queue: NSOpQ, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        if(error == nil){
            cell.theWPImage!.image = UIImage(data: data)
        }
        })
    return cell
}
Ricky
  • 10,435
  • 6
  • 34
  • 48
Bogdan Bogdanov
  • 902
  • 8
  • 34
  • 69

2 Answers2

3

I think you do not understand how the UITableView and UICollectionView delegate methods work.

iOS will not load 100 cells at the same time because it will be very slow and not a good user experience. So, the methods cellForItemAtIndexPath or cellForRowAtIndexPath are called only when the cells appear on the screen.

Whenever you scroll down, you will see the method cellForItemAtIndexPathis called again to make the new cells. If you put the code println(indexPath.row) inside cellForItemAtIndexPath, you will actually see the cell number is printed repeatedly when you scroll up or down.

If you see the same cells after you scroll down, it might be due to your code on cellForItemAtIndexPath. I am unable to help you unless you paste the full source code above. Or it might be a bug in UICollectionView for XCode GM seed.

How about trying the same project in XCode 6 Beta 7 and report back? I am having a problem with UICollectionView in XCode GM as well. IT is related to constraint. See: iOS 8 GM does not update constraints on collection views I am now using XCode 6 Beta 7 due to that.

Community
  • 1
  • 1
Ricky
  • 10,435
  • 6
  • 34
  • 48
1

Your problem is you are trying to load the images inside the cellForRow function. By the time image gets downloaded your collectionView is showing another cell. One of the right ways to do it is to load the images inside viewDidLoad in an array, then load from that array inside your cellForRow.