IOS UICollectionView选择问题
当用户点击IOS UICollectionView选择问题,ios,uicollectionview,ios8,xcode6,Ios,Uicollectionview,Ios8,Xcode6,当用户点击UICollectionViewCell时,我的UICollectionView有一个奇怪的行为 在我的假设中,根据苹果文档,当用户点击UICollectionViewCell时,单元格应该高亮显示,然后被选中 但在我的应用程序中,当用户点击手机时,手机只会高亮显示,而不是选中 当用户在单元格上滑动时,只有在这种情况下,单元格才会被选中 请帮忙。使用Xcode 6 我使用框中的UICollectionView和自定义UICollectionViewCell类,它覆盖setSelecte
UICollectionViewCell
时,我的UICollectionView
有一个奇怪的行为
在我的假设中,根据苹果文档,当用户点击UICollectionViewCell
时,单元格应该高亮显示,然后被选中
但在我的应用程序中,当用户点击手机时,手机只会高亮显示,而不是选中
当用户在单元格上滑动时,只有在这种情况下,单元格才会被选中
请帮忙。使用Xcode 6
我使用框中的UICollectionView和自定义UICollectionViewCell类,它覆盖setSelected
和setHighlighted
。我已经实现了这些方法
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
但这只是为了检查
UPD:
我已经拍下了录像 还应提供以下代码:
**UICollectionViewController**
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"should");
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"highlighted");
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"select %@", indexPath);
_selectedCategory = _source[(NSUInteger) indexPath.row];
// _selectedNumber = [NSNumber numberWithInteger:category.id];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"deselect %@", indexPath);
if (_selectedCategory) {
_selectedCategory = nil;
}
}
及
默认情况下,
UICapgesutureRecognitor
防止将触摸事件传播到UIView
。看
您可以通过取消选中IB中的“取消视图中的触摸”或按代码执行以下操作来禁用此功能:
UITapGestureRecognizer *recognizer = self.myTapGestureRecognizer;
recognizer.cancelsTouchesInView = NO;
确保在Overrided
setSelected:
和setHilighted:
中调用super
,并从collectionView:shouldSelectItemAtIndexPath:
返回YES
,当然我会这样做。将代码和视频添加到问题中因此,您的问题是collectionView:shouldSelectItemAtIndexPath:
甚至没有被调用。您是否使用任何UIgestureRecognitor
?我可以将您的评论标记为答案。我在整个视图上有一个点击识别器
,这导致了这个问题。也许你有什么想法,如何在整个视图中保持点击手势识别器
,同时允许在UICollectionView
中进行选择,就像js中的preventPropogation
一样,开箱即用,没有任何黑客攻击?
UITapGestureRecognizer *recognizer = self.myTapGestureRecognizer;
recognizer.cancelsTouchesInView = NO;