0

I have the following code, which puts a collection view into either one or two columns, depending the size of the screen:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let size = collectionView.frame.width
    if (size > 500) {
        return CGSize(width: (size/2) - 8, height: (size/2) - 8)
    }
    return CGSize(width: size, height: size)
}

I would like to amend this, so the height is dependent upon the reuseIdentifier. There are two I use - set something like this:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let diceRoll = Int(arc4random_uniform(2) + 1)
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("profileViewCell", forIndexPath: indexPath)
    if(diceRoll == 1) {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier("profileChartViewCell", forIndexPath: indexPath)
    }
    return cell
}

How can I get the reuseIndentifier of the current cell, so that I can change the height depending on what type of cell it is?

Ben
  • 4,707
  • 5
  • 34
  • 55

1 Answers1

0

reuseIdentifier is a property of UICollectionViewReusableView, which is UICollectionViewCell's base class. Thus you can call cell.reuseIdentifier at any time once you have a cell.

I'm not sure what your notion of "current cell" is. You can ask the collection view for a given cell at an index path with collectionView.cellForItemAtIndexPath(), and you can track which cell is currently selected by implementing the delegate method collectionView:didSelectItemAtIndexPath: and then saving off the currently selected indexPath.

Alternately (and what I'd recommend) is that you either subclass UICollectionViewCell and have the subclassed cell be responsible for its own height, or implement a custom UICollectionViewLayout class and handle size there.

If you implement your own cell subclasses, be sure to call registerClass:forCellWithReuseIdentifier: on your custom cell so the UICollectionView knows how to create it properly.

par
  • 17,361
  • 4
  • 65
  • 80