ios - UICollectionView 右侧高度单元格重叠

标签 ios image uicollectionview overlap uicollectionviewcell

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

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

这在一开始似乎有效,但一旦我向下滚动,图像就开始相互重叠。有什么办法可以避免这种情况吗?

enter image description here

最佳答案

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

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

关于ios - UICollectionView 右侧高度单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19511985/

相关文章:

ios - 如何从 Camera ios 拍摄多张图像

ios - 单击 UIImageView SWIFT 不工作

ios - 静态 TableViewCell 中的 UICollectionView

ios - 如何快速从 UItextfield 读取特定格式( XXX.X )?

ios - 将来自摄像头的视频 + 自定义 View 捕获到单个视频文件中

ios - 如何创建高度由其标签高度决定的 UITableView header ?

ios - 使用图像上传 Swift 动态调整 UICollectionViewCell 的大小

android studio屏幕尺寸在模拟器上不一样

javascript - 在上下文之间复制和操作图像数据

ios - 在 iOS 12 中,当使用具有约束的自调整大小单元格创建 UICollectionView 时,重新加载布局时高度错误,直到我滚动