【问题标题】:how to make dynamic size for the uicollection view cell in iOS?如何在 iOS 中为 uicollectionviewcell 制作动态大小?
【发布时间】:2016-01-04 07:34:40
【问题描述】:

我正在使用 uicollection 视图以网格视图格式显示图像。我可以显示单元格,但 iPad 的高度和宽度没有增加,它只显示为小块。请告诉我如何制作动态的。

[![在此处输入图片描述][1]][1]

我已使用以下代码使其动态化。

pragma mark 集合视图布局的东西

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

请告诉我如何解决这个问题?

【问题讨论】:

    标签: ios objective-c xcode ipad autolayout


    【解决方案1】:

    解决办法

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

    如果上述解决方案不能满足您的需求,要获得您想要的,您应该创建自己的 UICollectionViewLayout(或者可能是 UICollectionViewFlowLayout)子类,在其中执行将每个项目放置在正确框架中所需的所有计算。

    在此处查看great tutorial 和此处查看something similar to what you want

    【讨论】:

    • 这里的 YourCollectionViewObj 是什么?
    • @deepakkumar 它是您收藏视图的对象
    • 我没有为我的收藏视图容器制作任何单独的笔尖
    • 这样您就可以查看我提供的以下链接
    • 我需要一个快速的解决方案。
    【解决方案2】:

    如果你想增加单元格的高度和宽度,你必须使用sizeForItemAtIndexPath 方法。为此,您必须添加 UICollectionViewDelegateFlowLayout 代表。

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

    【讨论】:

    • 它会为所有设备制作动态尺寸。
    • 如果您想在所有设备的每行中显示 4 个单元格,然后传递 return CGSizeMake(collectionView.frame.size.width/4, collectionView.frame.size.width/4);,因此在所有设备中,根据您的 collectionview 宽度,它将减小并增加您的单元格高度和宽度。如果您想在特定设备中显示一些特定的高度和宽度,那么您必须检查设备是 5s、6 还是 ipad,并根据需要指定高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多