Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to highlight UICollectionViewCell: delegate or cell?
    text
    copied!<p>According to the <a href="http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html#//apple_ref/doc/uid/TP40012334-CH7-SW8" rel="noreferrer">Collection View Programming Guide</a> one should handle the visual state of the cell highlights in the <code>UICollectionViewDelegate</code>. Like this:</p> <pre><code>- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath]; [cell highlight]; } - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath]; [cell unhighlight]; } </code></pre> <p>What I don't like about this approach is that it adds logic to the delegate that is very specific to the cell. In fact, <code>UICollectionViewCell</code> manages its highlighted state independently, via the <code>highlighted</code> property.</p> <p>Wouldn't overriding <code>setHighlighted:</code> be a cleaner solution, then?</p> <pre><code>- (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { [self highlight]; } else { [self unhighlight]; } } </code></pre> <p>Are there any disadvantages to this approach instead of the delegate approach?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload