2013-02-14 80 views
10

我正在使用UICollectionView來顯示菜單,並且正在以非常奇怪的方式選擇這些項目。奇怪的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; 
} 

這裏是did select row方法,記錄標題和項目的行(它們全部在0節中):

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

最後,我的菜單的截圖:

enter image description here

這裏是日誌的時候我選擇第一項至第六,然後再從第六到第一(第一,第二,第三,第四,第五,六,六,五,四,三,二,一)(共12個水龍頭,注意第一個水龍頭甚至沒有註冊,也沒有第二個第六分接頭):

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

那是因爲你正在使用didDeselectItemAtIndexPath:方法而不是didSelectItemAtIndexPath:。一個容易犯的錯誤,尤其是如果你使用代碼完成時,你輸入英寸

+1

哇。我可能會尖叫一點,現在就太晚了。非常感謝你,真誠的! – Josh 2013-02-14 01:53:21

+0

@Josh,沒問題,這是我犯了一次以上的錯誤。 – rdelmar 2013-02-14 01:53:58

+0

哇。也犯了這個錯誤。認爲工作中有一些黑暗的魔法師。但不是。如果我自己上當了一大堆:) – villapossu 2013-02-20 15:17:30