UICollectionViewCell から (BOOL)isSelected {} をオーバーライドして、選択時のセルの外観を変更しています。セルをクリックしている場合、これは意図したとおりに機能します。しかし、選択を手動で設定している場合、isSelected メソッドは呼び出されません。これを解決するエレガントな方法はありますか?
カスタムセルのオーバーライドされたメソッド:
(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;
}
}
次のように、コントローラーでセルを手動で選択したい:
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];