IOS UICollectionView选择问题

时间:2014-09-22 10:51:13

标签: ios uicollectionview ios8 xcode6

当用户点击UICollectionView时,我的UICollectionViewCell会有一种奇怪的行为。

在我的假设中,根据apple docs,当用户点击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];
}

1 个答案:

答案 0 :(得分:3)

UITapGesutureRecognizer会阻止将触摸事件传播到UIView 默认。见the docs

您可以通过取消选中'取消视图中的触摸来禁用此功能。在IB或以下代码:

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