24

在我的 iphone 应用程序中,我在集合视图上显示图像。我从 url 获取图像,使用 url 我也得到图像大小。例如:-假设有风景和肖像两种图像。肖像图像的值是P,风景图像的值是L

如何自定义 collectionView 单元格的大小?

4

4 回答 4

64

使用此UICollectionViewDelegateFlowLayout方法

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
于 2013-04-17T13:55:20.670 回答
9

1) You could maintain and swap out multiple layout objects, but there's a simpler way. Just add the following to your UICollectionViewController subclass and adjust the sizes as required:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(170.f, 170.f);
    }
    return CGSizeMake(192.f, 192.f);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

Calling -performBatchUpdates:completion: will invalidate the layout and resize the cells with animation (you can just pass nil to both block params if you've no extra adjustments to perform).

2) Instead of hardcoding the item sizes in -collectionView:layout:sizeForItemAtIndexPath, just divide the height or width of the collectionView's bounds by the number of cells you want to fit on screen. Use the height if your UICollectionView scrolls horizontally or the width if it scrolls vertically.

于 2014-03-19T12:24:52.763 回答
2

这就是我所做的。我首先根据屏幕的大小设置单元格的大小(这样我们就可以在每一行上获得最佳的图像数量)。您可能需要更改数字以适合您的原因。

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat widthOfCell =(self.view.frame.size.width-30)/2;
CGFloat heightOfCell =widthOfCell * 1.33;
CGSize returnValue = CGSizeMake(widthOfCell, heightOfCell);
returnValue.height += 5; 
returnValue.width += 5; 
return returnValue;

}

然后我使用图像处理方法来确定横向或纵向状态,并且(对于我的应用程序)我决定将每张图片都设为纵向:

    if (sourceImage.size.width>sourceImage.size.height) {
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
                                             scale: 1.0
                                       orientation: UIImageOrientationRight];
    }

如果你想反过来,换><

注意:我的旋转方法没有考虑图片是指向左还是右。换句话说,如果图像是倒置的横向,我的代码将无法识别。我希望其他人可以帮助你,我希望我没有偏离轨道!:)

于 2015-09-06T23:48:06.520 回答
1

为了快速将流程布局扩展到您的视图控制器,如果您需要在单元格上显示动态图像,请制作变量,该变量将图像名称保存到您的视图控制器中并使用:

let imageData = ["image1","image2","image3","image4"]

extension yourViewController: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let photo = UIImage(named: imageData[indexPath.row])
        return CGSize(width: (photo?.size.width)!, height: (photo?.size.height)!)

}

}

于 2016-12-09T14:24:51.177 回答