codebot
codebot

Reputation: 2646

How to fetch images from NSDictionary to UICollectionView iOS?

I have a dictionary with few images. It's structured like following.

NSDictionary

 |__ bed
 |    |__ 351.jpg
 |    |__ 352.jpg
 |__ pillow
 |    |__ pillow1.png
      |__ pillow2.png 

I tried to add it to the colloectioview like following.

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.imagesDictionary.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[self.imagesDictionary allValues] objectAtIndex:section] count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    // Setup cell identifier
    static NSString *cellIdentifier = @"ImageCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    UIImage *image = (UIImage*)[[(NSArray*)[self.imagesDictionary allValues] objectAtIndex:indexPath.row] objectAtIndex:indexPath.row];
    recipeImageView.image = image;
    [cell addSubview:recipeImageView];
    // Return the cell
    return cell;
}

When I run this code, I got 2 sections and all section has same images(section1 - 351.jpg, pillow2.jpg, section2 - 351.jpg, pillow2.jpg). How can I fix this.

Thanks in Advance!

Upvotes: 1

Views: 1466

Answers (2)

Bardh Lohaj
Bardh Lohaj

Reputation: 1410

I wouldn't suggest you to store the items on NSDictionary in this case as you would need to search for sections with index(e.g 0) and not key(e.g bed). Therefore I would suggest ordered collections(NSArray). Check this Topic: Apple Documentation

Your main issue, seems to be this line of code:

UIImage *image = (UIImage*)[[(NSArray*)[self.imagesDictionary allValues] objectAtIndex:indexPath.row] objectAtIndex:indexPath.row];

because

  • the method allValues will not return the elements in a defined order
  • you are using indexPath.row instead of indexPath.section.

Upvotes: 1

ipmcc
ipmcc

Reputation: 29886

The problem here likely stems from the fact that NSDictionary is not an ordered container. If you want to pick items from it by index, you need to sort the keys before indexing into them. Try something like this:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.imagesDictionary.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray* sortedSections = [[self.imagesDictionary allKeys] sortedArrayUsingSelector: @selector(compare:)];
    NSString* key = sortedSections[section];
    NSArray* images = self.imagesDictionary[key];
    return images.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Setup cell identifier
    static NSString *cellIdentifier = @"ImageCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    NSArray* sortedSections = [[self.imagesDictionary allKeys] sortedArrayUsingSelector: @selector(compare:)];
    NSString* key = sortedSections[[indexPath indexAtPosition: 0]];
    NSArray* images = self.imagesDictionary[key];
    UIImage *image = (UIImage*) images[[indexPath indexAtPosition: 1]];
    recipeImageView.image = image;
    [cell addSubview:recipeImageView];
    // Return the cell
    return cell;
}

Upvotes: 3

Related Questions