2014-09-22 50 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;