sust86
sust86

Reputation: 1870

UICollectionView: Custom UI for a manually selected Cell

I'm overriding (BOOL)isSelected {} from UICollectionViewCell to change the cells appearance on selection. This does work as intend if I'm clicking on a cell. But the isSelected method never gets called if I'm setting the selection manually. Is there an elegant way to resolve this?

Overriden method in my custom cell:

 (BOOL)isSelected {
    if ([super isSelected]) {
        self.contentView.alpha = 0.2;
        self.contentView.backgroundColor = [UIColor greenColor];
        return YES;
    } else {
        self.contentView.alpha = 1.0;
        self.contentView.backgroundColor = [UIColor clearColor];
        return NO;
    }
}

I want to manually select an cell in my controller like this:

[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

Upvotes: 1

Views: 1335

Answers (1)

sust86
sust86

Reputation: 1870

My mistake was to override the method isSelected. I need to override the setter method setSelected everything works as expected.

- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
    self.contentView.alpha = 0.2;
    self.contentView.backgroundColor = [UIColor greenColor];
} else {
    self.contentView.alpha = 1.0;
    self.contentView.backgroundColor = [UIColor clearColor];
}

}

Upvotes: 1

Related Questions