今日はこの問題に遭遇しました。ここで私はそれをどのように解決したのですが、よりよい方法が必要です。コレクションビューの選択ロジックをセルコードに入れる必要はありません。
スクロールビューにUITapGestureRecognizerを追加します。
-(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];
}
}
}
}
}
:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[scrollView addGestureRecognizer:tapGesture];
その後、コールバックでは、セルの上に、通常のタップに何が起こるかをシミュレートする必要があり