我是新构建的 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);
}
这在一开始似乎有效,但一旦我向下滚动,图像就开始相互重叠。有什么办法可以避免这种情况吗?
最佳答案
您必须调整 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/