0

I am trying to display an array of images in a UICollectionView. The cells are being displayed, but the images are not.

Here is the cell being built:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:identifier
                                              forIndexPath:indexPath];


    UIImageView * catagoryImage = [[UIImageView alloc] initWithFrame:CGRectMake((cell.frame.origin.x),(cell.frame.origin.y), cell.frame.size.width, cell.frame.size.height)];


    [catagoryImage setImage:[UIImage imageNamed:[Apps objectAtIndex:indexPath.row]]];



    [cell.layer setCornerRadius:4];

    [cell addSubview:catagoryImage];


       cell.clipsToBounds = NO;

    return cell;


}

And here is the array being declared:

Apps  = [NSArray arrayWithObjects:@"Appstore.jpg", @"2.gif", @"3.gif", @"4.gif", @"5.gif", @"7.gif", @"8.gif",@"9.gif", nil];

Any ideas?

spogebob92
  • 1,474
  • 4
  • 23
  • 32

3 Answers3

0

You have a few options, but I'll name 2:

  1. This. It's a bit hacky. but it works

  2. Subclass UICollectionViewCell

Community
  • 1
  • 1
MendyK
  • 1,643
  • 1
  • 17
  • 30
0

There are two problems with the code. The first, bigger one, is the idea of creating an image view on every invocation of cellForItemAtIndexPath.

This method is called every time a cell is scrolled into view, and the cells are reused, so your cells will end up with piles and piles of UIImageViews on top of one another as the user scrolls around.

Problem two is the coordinate system of the imageView frame. It should be placed relative to the cell's bounds, not the cell's frame which is in the parent view's (collection view's) coordinate space, so...

// see if this cell has an image view already
UIImageView *categoryImageView = (UIImageView *)[cell viewWithTag:128];
if (!categoryImageView) {
    // only create one if we don't have one
    CGRect frame = cell.bounds;  // this is zero based, relative to the cell
    categoryImageView = [[UIImageView alloc] initWithFrame:frame];
    categoryImageView.tag = 128;  // so we can find it later
    [cell addSubview:categoryImageView];
}
// add it conditionally, but configure it unconditionally
categoryImageView.image = [UIImage imageNamed:[Apps objectAtIndex:indexPath.row]]];

Another possible problem with the code is access to the Apps objectAtIndex:. Apparently Apps is an unconventionally named instance of an NSArray. This is only okay if your numberOfItemsInSection datasource method returns Apps.count, and if all elements in the array are names of images in your app bundle.

danh
  • 62,181
  • 10
  • 95
  • 136
0

Sussed it.

The images were outside the keyboard scheme and were in the container package.

spogebob92
  • 1,474
  • 4
  • 23
  • 32
  • 1
    Glad its working. I'd call this a reasonable interpretation of "all elements in the array are names of images in your app bundle". Also, whatever else you do, please heed the advice regarding allocating new imageViews. Otherwise you're creating an unbounded number of them (i.e. add this `NSLog(@"subview count = %d", cell.subviews.count);` at the end of the method. Scroll around and watch your console. – danh Mar 11 '15 at 23:48
  • Massive thanks for the advice. Tried a few things out so the subviews are constantly replicating themselves. Cheers :) – spogebob92 Mar 12 '15 at 00:39