0

I have a UICollectionview in my UIViewController. On scroll collection view is not reusing cell. You can see numberOfItemsInSection method;

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

      return 7

    }

And this is the collectionViewLayout method;

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        if(collectionView == mainCollectionView){
            switch(indexPath.row){
            case 0:
                return CGSize(width: self.view.frame.width, height: self.view.frame.height * 0.20)
            case 1:
                return CGSize(width: (self.view.frame.width * 0.5) - 3, height: self.view.frame.height * 0.2)
            case 2:
                return CGSize(width: (self.view.frame.width * 0.5), height: self.view.frame.height * 0.2)
            case 3:
                return CGSize(width: self.view.frame.width, height: self.view.frame.height * 0.2)
            case 4:
                return CGSize(width: (self.view.frame.width * 0.6) - 3, height: self.view.frame.height * 0.2)
            case 5:
                return CGSize(width: (self.view.frame.width * 0.4), height: self.view.frame.height * 0.2)
            case 6:
                return CGSize(width: (self.view.frame.width), height: self.view.frame.height * 0.2)
            default:
                return CGSize(width: 0, height: 0)
            }
        }
    }

The last one is cellForRowAtIndexPath method:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if(collectionView == mainCollectionView){
            var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell

            switch(indexPath.row){
            case 0:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 1:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 2:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = false
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 3:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
            case 4:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 5:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = false
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 6:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            default:
                break
            }
            return cell
        }

Also you can see the output:

CollectionView Image

I have added a breakpoint to case6 bot it is not entering that case. Any help would be wonderful.

ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
  • change the Switch condition if there are 7 number of cells then use this switch((indexPath.row) % 7) – Fatti Khan May 18 '15 at 07:38
  • possible duplicate of [UICollectionView do not reuse cells](http://stackoverflow.com/questions/19276509/uicollectionview-do-not-reuse-cells) – Vizllx May 18 '15 at 07:39
  • Maybe the 7th item is out of the bounds of the screen, maybe you need to scroll a bit for the UICollectionView to load up that view? – Shai May 18 '15 at 07:40
  • I am scrolling the bottom to call the cellForItemAtIndexPath method but it is not reusing the cell. – ridvankucuk May 18 '15 at 07:41

2 Answers2

3

I don't think that's a good idea to handle each case like that.

You should create a wrapper object containing your cells parameters.

In your case, it should look something like the following :

class YourWrapperObject {
  var image: UIImage
  var clockIconHidden: Bool
  var size: CGSize
  // Your other vars
}

create an array of those objects (in your viewDidLoad method for example), then, in your cellForRowAtIndexPath method:

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

  var cell = /*get your cell using reusable identifier*/
  let cellParameters = yourObjectArray[indexPath.row]

  /* Then assign each parameters */
  cell.imageView.image = cellParameters.image
  cell.clockIconImageView.hidden = cellParameters.clockIconHidden
  cell.pinIconImageView.hidden = cellParameters.pinIconHidden
  cell.placeLabel.hidden = cellParameters.placeHidden
  cell.lineView.hidden = cellParameters.lineHidden

  /* return your cell */
  return cell
}

as well as in your sizeForItemAtIndexPath method:

return yourObjectArray[indexPath.row].size

Your code will be much more readable and flexible

Slash705
  • 56
  • 3
0

Reloading UICollectionView data in the main thread solved my problem. I used:

dispatch_async(dispatch_get_main_queue()) {
                    mainCollectionView.reloadData()
                }
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41