1

My UICollectionView has a UIImageView with autolayout constraints. I require a fixed number of cells in a row so need to scale the image if cell requires.

Even if I do not resize the cells, but set UIimageView in storyboard larger than the cells expected dimension the first time cell is populated the image size is incorrect, however when each cell is reused afterwards they scale perfectly.

The scaling works once cells are reused, but please what am I missing?

example view is the second call to reloaddata on the 'balls' lower middle of view. The first call filled the first 4 cells. The second call corrects the now reused cells, but fails to scale the 4 new cells content.

Below is some code as requested. generalBreakCollection is in focus :

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


if (collectionView == self.p1HiBreakCollection) {

    breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"P1Cell" forIndexPath:indexPath];

    UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:106];
    ballShot *selectedBall = [self.p1BreakBalls objectAtIndex:indexPath.row];
    cell.ball = [self.p1BreakBalls objectAtIndex:indexPath.row];
    collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];


    return cell;
} else if (collectionView == self.p2HiBreakCollection) {

    breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"P2Cell" forIndexPath:indexPath];

    UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:108];
    ballShot *selectedBall = [self.p2BreakBalls objectAtIndex:indexPath.row];
    cell.ball = [self.p2BreakBalls objectAtIndex:indexPath.row];
    collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];


    return cell;

} else {
    // generalBreakCollection


    breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"GeneralCell" forIndexPath:indexPath];

    ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
    cell.ball = [self.breakShots objectAtIndex:indexPath.row];


    cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
    cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:@"highlighted_%@",selectedBall.imageNameLarge]];

    cell.ballStoreImage.contentMode = UIViewContentModeScaleAspectFit;
    return cell;
}
}

The resize block contains:

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout

sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

if (collectionView == self.generalBreakCollection) {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    float cellWidth = screenWidth / 10.0; //Replace the divisor with the column count requirement. Make sure to have it in float.
    CGSize size = CGSizeMake(cellWidth, cellWidth);  
return size;  
}
else {
    CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];
    return defaultSize    ;
}

}

I have my own UICollectionViewCell class with a UIImageView property ballStoreImage. I do not do so much with it, but I have linked it to the UIImageView inside the cell in the storyboard:

@property (strong, nonatomic) IBOutlet UIImageView *ballStoreImage;
drewglew
  • 105
  • 1
  • 10
  • Can you show us some code? – Beau Nouvelle Jan 24 '16 at 08:59
  • 1
    Found solution on this link: [link](http://stackoverflow.com/questions/25804588/auto-layout-in-uicollectionviewcell-not-working) The autolayout was not getting recognised until I added the code line: [[cell contentView] setFrame:[cell bounds]]; – drewglew Jan 24 '16 at 14:24

1 Answers1

2

The autolayout was not getting recognised until I added the code line:

[[cell contentView] setFrame:[cell bounds]];

So full block reads:

    } else {
    breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"breakCell" forIndexPath:indexPath];

    [[cell contentView] setFrame:[cell bounds]];


    ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
    cell.ball = [self.breakShots objectAtIndex:indexPath.row];

    cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
    cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:@"highlighted_%@",selectedBall.imageNameLarge]];
    return cell;

}
drewglew
  • 105
  • 1
  • 10