Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange UICollectionView Selection Behavior

I'm using a UICollectionView to display a menu, and the items are being selected in a VERY strange way.

Here is my static data that populates them:

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

And the cell provider, where the custom subclass is just attached to the prototype cell and has a UILabel and a UIImageView:

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

Here is the did select row method, logging the title, and the row of the item (they're all in section 0):

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

Lastly, the screenshot of my menu:

enter image description here

Here is the log when I select the items from First to Sixth, then back from Sixth to First (First, Second, Third, Fourth, Fifth, Sixth, Sixth, Fifth, Fourth, Third, Second, First) (12 total taps, note that the first tap doesn't even register, nor does the second Sixth tap):

------------------------------------- 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
like image 340
Josh Avatar asked Feb 14 '13 01:02

Josh


1 Answers

That's because you are using didDeselectItemAtIndexPath: method instead of didSelectItemAtIndexPath:. An easy mistake to make, especially if you're using code completion when you type it in.

like image 110
rdelmar Avatar answered Sep 22 '22 04:09

rdelmar