【问题标题】:Setting height for UICollectionViewCell为 UICollectionViewCell 设置高度
【发布时间】:2018-01-24 04:05:07
【问题描述】:

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

中设置了 UILabel 高度
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath

然后我在

中设置 UICollectionViewCell 高度
- (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 cellForItemAtIndexPath:(NSIndexPath *)indexPath .时i的高度为零。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      布局委托方法- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
      先在datasource方法-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath之前执行,sizeForItemAtIndexPath计算出所有cell的大小,然后开始构建。

      所以,你应该只通过 indexPath 获取内容,然后计算显示高度,应用 cellForItemAtIndexPath 中的高度值

      【讨论】:

        【解决方案3】:

        您尝试实现的是 Self Sizing UICollectionViewCells。要实现它,您需要:

        • 为 flowLayout 设置estimatedItemSize
        • 将标签固定到顶部、底部、前缘和后缘
        • 确保最后一个标签的 Vertical Content Hugging Priority 是 250
        • 并在您的 UICollectionViewCell 中实现 preferredLayoutAttributesFitting

        我在这里写了一篇关于 Self Sizing UICollectinViewCells 的更长的博客文章: http://parveenkaler.com/posts/self-sizing-uicollectionviewcell-swift

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-02
          • 2016-10-27
          • 2015-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多