【问题标题】:UICollectionView cell individual heightUICollectionView 单元格个体高度
【发布时间】:2014-12-10 19:50:18
【问题描述】:

我有一个UICollextionView,它目前计算出UILabel 的高度。每个标签都有自己的高度。我现在想对集合视图单元做同样的事情。我似乎只能将它们全部更改为相同的。我该怎么办?谢谢

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as ChatCellCollectionViewCell


    cell.backgroundColor = UIColor.whiteColor()
    cell.layer.cornerRadius = 10

    cell.textLabel.text = "\(messageArray[indexPath.row])"
    cell.nameLabel.text = "\(namesArray[indexPath.row])"


    cell.textLabel.numberOfLines = 0
    cell.textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
    frame = cell.textLabel.frame
    cell.textLabel.sizeToFit()
    frame.size.height = cell.textLabel.frame.size.height
    cell.textLabel.frame = frame


    return cell
}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    如果您使用的是流式布局,UICollectionViewDelegateFlowLayout 协议有一个名为collectionView:layout:sizeForItemAtIndexPath: 的函数,可以让您执行此操作。

    【讨论】:

    • 我应该在哪里使用这条线?而不是collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 或在上面的代码中?
    • 您可以在实现cellForItemAtIndexPath 的同一视图控制器中执行此操作,尽管您需要在该类中声明对UICollectionViewDelegateFlowLayout 的支持'@implementation
    • 所以我应该使用它来代替collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
    • 你会想要同时使用这两个 - cellForItemAtIndexPath 提供单元格,sizeForItemAtIndexPath 提供相应的大小。可能值得阅读 Apple 的 Collection View Programming guide developer.apple.com/library/ios/documentation/WindowsViews/…
    【解决方案2】:

    可以通过 UICollectionViewFlowlayout 来完成,见这里:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html

    滚动到指定流布局中项目的大小

    【讨论】:

      【解决方案3】:

      将此方法添加到您的视图控制器

      - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
      {
      
      CGFloat width=0.0;
      CGFloat height=0.0;
      
      if (IS_IPHONE_5){
          width=90;
          height=120;
      }else if (IS_IPHONE_6){
          width=107;
          height=130;
      
      }else if (IS_IPHONE_6_PLUS){
          width=125;
          height=150;
      
      }else{
          width=90;
          height=120;
      
      }
      
      return CGSizeMake(width, height);
      

      }

      在设备检测的地方可以使用 indexPath.row ..like

      if (indexPath.row==0){
        // Your Code
        }
      

      注意:它返回 CGSize..

      希望这个回答对你有帮助。

      【讨论】:

      • 这是可行的,但是我需要从内容中获取单元格的高度。有什么办法可以给一个临时值,一旦内容设置好就可以调整。
      • OK 现在你可以添加单元格大小 = Your content.frame.size.height
      • 我是在上面的代码中添加这个还是在我设置单元格内容的地方添加这个?
      • 这个链接对你有帮助..cocoanetics.com/2013/08/…你的网站很好..
      猜你喜欢
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 2020-12-27
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多