Mi sono imbattuto in questo oggi. Ecco come l'ho risolto, ma deve esserci un modo migliore. Non avrei dovuto inserire la logica di selezione della vista raccolta nel mio codice cella.
Aggiungere un UITapGestureRecognizer alla visualizzazione di scorrimento.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[scrollView addGestureRecognizer:tapGesture];
Poi, nella richiamata, si deve simulare quello che accadrebbe in un normale rubinetto sulla cella:
-(void) scrollViewTapped:(UITapGestureRecognizer *)sender {
UIView *tappedView = [sender view];
while (![tappedView isKindOfClass:[UICollectionView class]]) {
tappedView = [tappedView superview];
}
if (tappedView) {
UICollectionView *collection = (UICollectionView *)tappedView;
NSIndexPath *ourIndex = [collection indexPathForCell:self];
BOOL isSelected = [[collection indexPathsForSelectedItems] containsObject:ourIndex];
if (!isSelected) {
BOOL shouldSelect = YES;
if ([collection.delegate respondsToSelector:@selector(collectionView:shouldSelectItemAtIndexPath:)]) {
shouldSelect = [collection.delegate collectionView:collection shouldSelectItemAtIndexPath:ourIndex];
}
if (shouldSelect) {
[collection selectItemAtIndexPath:ourIndex animated:NO scrollPosition:UICollectionViewScrollPositionNone];
if ([collection.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
[collection.delegate collectionView:collection didSelectItemAtIndexPath:ourIndex];
}
}
} else {
BOOL shouldDeselect = YES;
if ([collection.delegate respondsToSelector:@selector(collectionView:shouldDeselectItemAtIndexPath:)]) {
shouldDeselect = [collection.delegate collectionView:collection shouldDeselectItemAtIndexPath:ourIndex];
}
if (shouldDeselect) {
[collection deselectItemAtIndexPath:ourIndex animated:NO];
if ([collection.delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]) {
[collection.delegate collectionView:collection didDeselectItemAtIndexPath:ourIndex];
}
}
}
}
}
fonte
2014-03-07 22:51:55