wtorsi
wtorsi

Reputation: 732

IOS UICollectionView Selection issue

My UICollectionView has a strange behavior, when user taps on UICollectionViewCell.

In my assumption, according to apple docs, when user taps on UICollectionViewCell, cell should become highlighted and then selected.

But in my app, when user taps on cell, it only becomes highlighted, not selected.

And when user swipes on cell, and only in this case, cell becomes selected.

Any help, please. Xcode 6 is used.

I use UICollectionView from the box, with custom UICollectionViewCell class, which overrides setSelected and setHighlighted. I've implemented these methods

- (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 

but only for check.


UPD:
I've captured the video http://take.ms/LzBkZ.

Also provide the code:

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

} 

And

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

Upvotes: 1

Views: 1661

Answers (1)

rintaro
rintaro

Reputation: 51911

A UITapGesutureRecognizer prevents propagating touch event to UIView by default. see the docs

You can disable this feature by unchecking 'Cancels touches in view' in IB or following by code:

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

Upvotes: 3

Related Questions