According to the Collection View Programming Guide one should handle the visual state of the cell highlights in the UICollectionViewDelegate
. Like this:
- (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];
}
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, UICollectionViewCell
manages its highlighted state independently, via the highlighted
property.
Wouldn't overriding setHighlighted:
be a cleaner solution, then?
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
if (highlighted) {
[self highlight];
} else {
[self unhighlight];
}
}
Are there any disadvantages to this approach instead of the delegate approach?