How to make dynamic cell height of UICollectionview?

I have one UIcollectionview. There are several collection cells. Inside CollectionviewCellthere is a text image. The content of the textview is dynamic. Therefore, the cell height must be dynamic.

How to do it?

+4
source share
4 answers

1-Add protocol UICollectionViewDelegateFlowLayout.
2-Comply with the protocol by implementing method sizeForItemAtIndexPathc ViewController.
3. Use the cell index to get the text you have and calculate its height and width.

class ViewController: UIViewController,UICollectionViewDelegateFlowLayout{

  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
     let constraintRect = CGSize(width: self.view.frame.size.width, height: CGFloat.max)
     let data = myTextArray[indexpath.row];
     let boundingBox = data.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: Constants.strings.fontName, size: 30)!], context: nil)
     return CGSizeMake(boundingBox.width, boundingBox.height); //(width,hight)
  }

}
+3
source

NSAttributedString sizeForItemAtIndexPath rect :

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let attString = NSAttributedString(string: self.rows[indexPath.row], attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15.0)])
    var r: CGRect = attString.boundingRectWithSize(CGSizeMake(self.collectionView.bounds.size.width, CGFLOAT_MAX), options: .UsesLineFragmentOrigin, context: nil)

    return r.size

}
+2

1) Override the collectionflowlayout delegate method

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(50, 50); //(width,hight)
}

2) In the above method, calculate the height of the text view that contains the dynamic length, and add this value to the fixed height. Ex. cellHeight = 100 + textview height. //100 is height which not contain textview height.

3) After calculating the height, replace it with the method described above

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSizeMake(50, dynamicheight); //(width,hight)
    }
0
source

in objective-c

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
      if ()//your condition
        {
            return 280;
        }
        else
        {
            return 330;
        }

    }

in quick

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
    {
       //check your conditions here ex: device, iOS version then return
       return CGSizeMake(64, 64)
    }
0
source

Source: https://habr.com/ru/post/1650114/


All Articles