Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置UICollectionViewCell的高度_Ios_Objective C_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 设置UICollectionViewCell的高度

Ios 设置UICollectionViewCell的高度,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我想根据单元格中内容的大小(特别是UILabel)更改UICollectionViewCell的高度。我将UILabel的高度设置为 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath - (CGSize)collectionView:(UICollectionView *)collectionView

我想根据单元格中内容的大小(特别是UILabel)更改UICollectionViewCell的高度。我将UILabel的高度设置为

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
然后在中设置UICollectionViewCell高度

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
似乎在第一个方法之前调用了第二个方法,因此尚未计算标签的高度。在下面找到完整的代码警告:需要一个整洁的!。有什么想法吗

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *myCell = [collectionView
                                    dequeueReusableCellWithReuseIdentifier:@"Cell"
                                    forIndexPath:indexPath];


    label = [[UILabel alloc] initWithFrame:CGRectMake(80,10,width - 90,0)];
    label.text = @"Hellfoneofinerigoneriognwirotngwtngowirnthointgonowenfkqejb fgjkreb glknrlegkn ewlj qerlgjnweofjpeorihgpireghiperhgorgrngl;rtnh;ltm;l";
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:17];
    [label sizeToFit];

    [myCell.contentView addSubview:label];
    CGSize labelY = [label bounds].size;
    i = labelY.height;
    NSLog(@"Height: %d", i);


        return myCell;
}

#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

 NSLog(@"Height: %d", i);

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 568)
        {
           return CGSizeMake(300.f, i);

        }
        if(result.height == 667)
        {
          return CGSizeMake(340.f, i);

        }
        if(result.height == 736)
        {
          return CGSizeMake(370.f, i);


        }
    }
    return CGSizeMake(300.f, i);

}
编辑:没有真正解释错误。当-UICollectionViewCell*collectionView:UICollectionView*collectionView时,i的高度等于零
cellForItemAtIndexPath:NSIndexPath*indexPath。调用。

通过在height方法中创建标签并确定其中的高度来工作。

布局委托方法-CGSizecollectionView:UICollectionView*collectionView布局:UICollectionViewLayout*collectionViewLayout大小FormiteIndepath:nsIndepath*Indepath将 首先在datasource方法-UICollectionViewCell*collectionView:UICollectionView*collectionView cellForItemAtIndexPath:NSIndexPath*indexPath之前执行,然后开始构建它们


因此,您应该通过indexPath获取内容,然后计算显示的高度,在cellForItemAtIndexPath中应用高度值,您试图实现的是自调整UICollectionViewCells的大小。要实现这一点,您需要:

为flowLayout设置estimatedItemSize 将标签固定到顶部、底部、前缘和后缘 确保最后一个标签的垂直内容包含优先级为250 并在UICollectionViewCell中实现preferredLayoutAttributesFitting 我在这里写了一篇关于自我调整UICollectionInvewCells大小的较长博文: