0

我是新建筑 UICollectionView ,我有一个问题。我想显示宽度为 300 像素的图像并计算正确的高度比例。为此,我做了:

- (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);
}

这似乎在一开始就有效,但是一旦我向下滚动,图像就会开始相互重叠。这是避免这种情况的任何方法吗?

在此处输入图像描述

4

1 回答 1

1

您必须调整 UICollectionView 中的单元格大小,使用 UICollectionViewFlowLayout

查看 Ray Wenderlich 教程:http ://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

于 2013-10-22T08:03:55.487 回答