2013-02-14 4 views
10

UICollectionViewを使用してメニューを表示していて、項目が非常に奇妙な方法で選択されています。ここでStrange UICollectionViewの選択動作

はそれらを移入し、私の静的なデータである:

self.menuItems = @[@{@"text" : @"First", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Second", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Third", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Fourth", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Fifth", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Sixth", @"image" : @"180-stickynote.png"}]; 

とカスタムサブクラスはちょうどプロトタイプセルに取り付けられ、UILabelUIImageViewを持っているセル・プロバイダー、:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

    CUMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCell" forIndexPath:indexPath]; 

    NSDictionary *cellInfo = [self.menuItems objectAtIndex:indexPath.row]; 

    cell.imageView.image = [UIImage imageNamed:[cellInfo valueForKey:@"image"]]; 

    cell.label.text = [cellInfo valueForKey:@"text"]; 

    return cell; 
} 

ここでは、選択された行方法があり、タイトルと項目の行を記録します(すべてセクション0にあります)。

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%@: %d", [[self.menuItems objectAtIndex:indexPath.row] valueForKey:@"text"], indexPath.row); 
} 

最後に、私のメニューのスクリーンショット:私は第六に、当時六から第一に(第一、第二、第三、第1〜第4から項目を選択すると、ここで

enter image description here

は、ログです第5、第6、第六、第五、第四、第三に、第1、第2、第)(合計12回のタップ、最初のタップにも登録、及び第2第六のタップをしていないことに注意してください):あなたのため

------------------------------------- FIRST TAP ON FIRST HERE 
2013-02-13 19:38:37.343 App[1383:c07] First: 0 // second tap, on Second 
2013-02-13 19:38:38.095 App[1383:c07] Second: 1 // third tap, on Third 
2013-02-13 19:38:38.678 App[1383:c07] Third: 2 // fourth tap, on Fourth 
2013-02-13 19:38:39.375 App[1383:c07] Fourth: 3 // fifth tap, on Fifth 
2013-02-13 19:38:40.167 App[1383:c07] Fifth: 4 // so on 
2013-02-13 19:38:41.751 App[1383:c07] Sixth: 5 
------------------------------------- SECOND TAP ON SIXTH HERE 
2013-02-13 19:38:42.654 App[1383:c07] Fifth: 4 
2013-02-13 19:38:43.318 App[1383:c07] Fourth: 3 
2013-02-13 19:38:44.495 App[1383:c07] Third: 2 
2013-02-13 19:38:45.071 App[1383:c07] Second: 1 

答えて

52

だことdidSelectItemAtIndexPath:の代わりにdidDeselectItemAtIndexPath:メソッドを使用しています。特にコード補完を使用している場合は、入力ミスが簡単です。

+1

ワウ。私はちょっと、あまりにも遅くまで悲鳴を上げるかもしれない。本当にありがとう、誠実に! – Josh

+0

@Josh、問題はない、それは私が何度も作った間違いだ。 – rdelmar

+0

うわー。この間違いを犯した仕事で暗い魔法があったと思った。しかし、いいえ。私は大きな時間をだまされていました:) – villapossu

関連する問題