2

I have a custom UICollectionViewCell, and I dequeue it from my view controller by registering it like so

[self.calendarView registerNib:[UINib nibWithNibName:NSStringFromClass([DayCell class]) bundle:nil] forCellWithReuseIdentifier:dayCell];

and then dequeueing like so

cell = [collectionView dequeueReusableCellWithReuseIdentifier:dayCell forIndexPath:indexPath];

My question is, which UICollectionViewCell init method would allow me to access the cell's reuseIdentifier? Both awakeFromNib and initWithCoder: methods get called, however, both log (null) for self.reuseIdentifier.

This is a problem, because I want to use the same UICollectionViewCell class but with different reuseIdentifiers to achieve slightly differently looking cells, and I want to perform the styling once upon init. For instance, a cell with dayCellDisabled reuseIdentifier would have a label of lighter colour.

artooras
  • 6,315
  • 9
  • 45
  • 78
  • Why not configure the color in Interface Builder? – jlehr Feb 20 '15 at 19:56
  • 1
    Because I want to apply a different colour based on the reuseIdentifier – artooras Feb 20 '15 at 21:07
  • I'm not sure I follow. Each reuse identifier has to be associated with a separate instance of `UICollectionViewCell`, so why not apply a color to each of the cells in IB, since you're creating them there anyway? – jlehr Feb 21 '15 at 18:14
  • 1
    Say, I want to have the same custom complex cell with text in two different colours. It would be a wasteful effort creating two separate xibs just to change the label colour and assign different reuseIdentifiers in IB. So, I wanted to set the different label colour based on reuseIdentifier in code. – artooras Feb 22 '15 at 08:39

1 Answers1

2

One solution I found that works is to not override any init or awake methods, but configure my reusable view in this method instead

- (void)didMoveToSuperview {};

Apparently, this method gets called only once when the view is being added to its handler. I tested and confirmed this is the case for a reusable UICollectionViewCell that is added to a UICollectionView.

artooras
  • 6,315
  • 9
  • 45
  • 78
  • So you define a property and depending on the value you initialize your cell differently in `didMoveToSuperview`? I noticed that `didMoveToSuperview` is not called for every cell which is displayed in the collection view. So cell reuse does take place. What does that mean for the application? – testing Sep 02 '15 at 14:34
  • I think you have used the reuse identifier. Depending on the value you choose this design or the other. Clever! Is there a performance issue if you do that in `didMoveToSuperView` instead of the initializer? – testing Sep 02 '15 at 15:02
  • Well, if a cell is being reused, then you'll probably want to keep the same configuration as when initialised, so that's not an issue. And you apply the actual values (strings, images) in the view controller that is the dataSource of the tableview. As for performance issues, I haven't tested - no obvious slow-downs though. – artooras Sep 05 '15 at 07:10