UICollectionView indexPath.row or item, items not stable, jumping around

10,718

Solution 1

I think you are missing one delegate methods that defines how many section will your collection view have?

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1; 
}

and one the more change the method didDeSelect for didSelect...

Solution 2

You have implemented didDeselectItemAtIndexPath:, not didSelectItemAtIndexPath:. Therefore selecting a cell logs the index path of the previously selected cell.

Share:
10,718
denikov
Author by

denikov

Aspiring developer, learning to program and code on the fly. Thanks everyone for helping me with my questions

Updated on June 09, 2022

Comments

  • denikov
    denikov almost 2 years

    I created a collection view and I'm loading images into it. Problem I'm running across is when I click on a cell to get it's indexPath.item or indexPath.row, the numbers are all screwed up. Clicking on the very first image doesn't display anything, clicking on the next will give me 0. Clicking on the third will give me a 1, and clicking back on the second one gives me a 2. This happens all throughout the view. Has to be something wrong that I'm doing. Here's my code:

    viewDidLoad:

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    [layout setItemSize:CGSizeMake(75, 75)];
    self.images = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 200) collectionViewLayout:layout];
    self.images.delegate = self;
    self.images.dataSource = self;
    [self.images registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.view addSubview:self.images];
    

    delegate methods:

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.imagesGallery.count; //image paths for server
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [[UIColor whiteColor]CGColor];
        UIImageView *image = [[UIImageView alloc]init];
        image.image = [UIImage imageWithData:[self.imagesGallery objectAtIndex:indexPath.row]];
        image.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
        [cell addSubview:image];
        return cell;
    }
    
    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"%d", indexPath.item);
    }
    

    I'm not sure how to get exact control of what goes where. Are they numbered normally like:

    0 1 2
    3 4 5
    ....
    

    Or is there a special way they get numbered based on the sections? How do they get populated?