简体   繁体   English

如何在iOS中为uicollection视图单元设置动态大小?

[英]how to make dynamic size for the uicollection view cell in iOS?

I am using uicollection view to display the images in grid view format.I am able to display the cells but the height & width is not increased for iPad it only display as the small blocks.Please tell me how can i make it dynamic. 我正在使用uicollection视图以网格视图格式显示图像。我能够显示单元格,但iPad的高度和宽度没有增加,只能显示为小块。请告诉我如何使它动态化。

[![enter image description here][1]][1] [![在此处输入图片描述] [1]] [1]

I have used following code for make it dynamic. 我使用以下代码使其动态化。

pragma mark Collection view layout things 实用标记集合视图布局东西

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView==self.album_collection_view) {
        return 2.0;
    }
    else
        return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView==self.album_collection_view) {
        return 2.0;
    }
    else
        return 2.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    if (collectionView==self.album_collection_view) {
        return UIEdgeInsetsMake(5,0,0,0);
    }// top, left, bottom, right
    else
        return  UIEdgeInsetsMake(5,0,0,0);

}

Please tell me how can i resolve this issue? 请告诉我如何解决此问题?

here is the solution 这是解决方案

 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

YourCollectionCell * cell = (YourCollectionCell *) [YourCollectionViewObj cellForItemAtIndexPath:indexPath];

        if (cell == nil) {

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCollectionCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
            cell.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20);
            // SET YOUR CONTENT
            [cell layoutIfNeeded];

        }

        CGSize CellSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow];


        return CellSize;}

In case above solution not meet your needs,To obtain what you want, you should create your own UICollectionViewLayout (or maybe UICollectionViewFlowLayout) subclass, where you perform all the computations needed for placing every item in the right frame. 如果上述解决方案无法满足您的需求,则要获取所需的内容,您应该创建自己的UICollectionViewLayout(或UICollectionViewFlowLayout)子类,在其中执行将每个项目放置在正确框架中所需的所有计算。

Take a look here for a great tutorial and here for something similar to what you want . 在这里查看出色的教程 ,在此处查看与所需内容类似的内容

If you want to increase cell height and width you have to use sizeForItemAtIndexPath method. 如果要增加单元格的高度和宽度,则必须使用sizeForItemAtIndexPath方法。 for that you have to add UICollectionViewDelegateFlowLayout delegate. 为此,您必须添加UICollectionViewDelegateFlowLayout委托。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM