2014-09-22 53 views
1

我的UICollectionView有一个奇怪的行为,当用户点击UICollectionViewCellIOS UICollectionView选择问题

在我的假设中,根据苹果文档,当用户点击UICollectionViewCell时,单元格应该变亮,然后选中。

但在我的应用程序中,当用户点击单元格时,它只会变亮,而不会被选中。

而当用户在单元格上滑动时,只有在这种情况下,单元格才会被选中。

任何帮助,请。使用Xcode 6。

我使用UICollectionView从自定义UICollectionViewCell类,它覆盖setSelectedsetHighlighted。我已经实施了这些方法

- (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:
我拍摄的视频http://take.ms/LzBkZ

另外提供的代码:

**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; 
    } 

} 

而且

**SSCustomViewCell** 
- (void)setSelected:(BOOL)selected 
{ 
    [super setSelected:selected]; 

    self.alpha = (CGFloat) (selected ? 0.4 : 1); 

    [self setNeedsDisplay]; 

} 

- (void)setHighlighted:(BOOL)highlighted 
{ 
    [super setHighlighted:highlighted]; 

    self.alpha = (CGFloat) (highlighted ? 0.5 : 1); 
    [self setNeedsDisplay]; 
} 
+0

确保您调用'super'在重写'的setSelected:'和'setHilighted:' ,并从'collectionView:shouldSelectItemAtIndexPath:' – rintaro 2014-09-22 11:22:05

+0

返回'YES'当然,我这样做。将代码和视频添加到问题 – wtorsi 2014-09-22 11:31:15

+1

因此,您的问题是“collectionView:shouldSelectItemAtIndexPath:”甚至没有被调用。你使用了什么'UIGestureRecognizer'? – rintaro 2014-09-22 14:22:47

回答

3

UITapGesutureRecognizer防止默认传播触摸事件UIView。看到IB the docs

您可以通过取消选中禁用此功能,“取消触摸鉴于”或代码如下:

UITapGestureRecognizer *recognizer = self.myTapGestureRecognizer; 
recognizer.cancelsTouchesInView = NO;