Alexandre
Alexandre

Reputation: 410

UICollectionView right height cell overlap

I'm new building UICollectionView and I have an issue. I would like to display images that have 300px width and calculate the right height scale. To do this, I did :

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

UIImage *image = [photos objectAtIndex:indexPath.row];
if(image){

    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];

    float scale = (image.size.height/image.size.width)*300;

    [imageView setFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, 300, scale )];

    [cell addSubview:imageView];
    cell.backgroundColor = [UIColor clearColor];
}else{

    cell.backgroundColor = [UIColor redColor];
}


return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = [photos objectAtIndex:indexPath.row];
    float scale = (image.size.height/image.size.width)*300;
    return  CGSizeMake(300, scale);
}

This seems to work at the beginning but as soon as I scroll down, images start to overlap each others. Is that any way to avoid this ?

enter image description here

Upvotes: 0

Views: 1425

Answers (1)

Karthik Sivam
Karthik Sivam

Reputation: 43

You have to resize cells in UICollectionView, use UICollectionViewFlowLayout

Check out the Ray Wenderlich Tutorial: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

Upvotes: 1

Related Questions