简体   繁体   English

UICollectionView单元格动态高度

[英]UICollectionView Cell dynamic height

I have created an UICollectionView cell and it's displaying perfectly, but my doubt is have given an constant size of cell in sizeForItemAtIndexPath . 我已经创建了一个UICollectionView单元格,并且它可以完美显示,但是我怀疑在sizeForItemAtIndexPath中单元格的大小是否恒定。 My issue is each and every cell is containing dynamic height, how can i calculate and give at sizeForItemAtIndexPath 我的问题是每个单元格都包含动态高度,我该如何计算并给出sizeForItemAtIndexPath

You can implement the function like so: 您可以像这样实现该功能:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  {
       NSString* blogEntry = (NSString*)self.entries[indexPath.item];
       CGSize size = [blogEntry sizeWithAttributes:
                                @{NSFontAttributeName:font}];
       return HEADER_HEIGHT + size.height;
   }

Inside you want to calculate the size of the cell. 在内部要计算单元格的大小。 One common scenario is a cell that has a header and a body of text (like a post in a blog). 一种常见的情况是具有标题和正文的单元格(例如博客中的帖子)。 In this case you need to calculate the text using sizeWithAttributes: method and add the standard header size. 在这种情况下,您需要使用sizeWithAttributes:方法计算文本并添加标准标题大小。

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

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