How to change dynamic cell height of UICollectionView in iOS

I create on UICollectionViewto display some data coming from the server. But I have to set the dynamic height of the cell according to the size of the text. I am creating a custom cell for UICollectionView.

Get this cell UICollectionViewin my method ViewDidLoad:

UINib *cellNib = [UINib nibWithNibName:@"Custom_CollectionViewCell" bundle:nil];
[self.noteBookmarkCollectinView registerNib:cellNib forCellWithReuseIdentifier:@"CustomCell"];

Below is the delegate method for UICollectionView:

 #pragma mark Collection view layout things

 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
 }
 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
    return noteDetailsArr.count;
 }    
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
 }
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
 }
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
 {
    return UIEdgeInsetsMake(0,4,0,4);  // top, left, bottom, right
 }
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    return CGSizeMake(154, 154); // This is my fixed Cell height

   //Before I am trying this below code also, but its not working
    return [(NSString*)[contentArr objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
 }
 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *cellIdentifier = @"CustomCell";

    Custom_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
   cell.contentLbl.numberOfLines = 0;
   cell.contentLbl.tag = indexPath.row;

   cell.contentLbl.text = [contentArr objectAtIndex:indexPath.row];
   [cell.contentLbl sizeToFit];

   return cell;
}

My result will be as follows:

enter image description here

Notes: green is the background color of the cell, and pink is the BG Color label.

And when I scroll CollectionView, I can see, as shown below, the Image and After scrolling it is beautiful again (Only for the last line)

enter image description here

How can I make the cell height dynamically according to the text. please, help. I am stuck from last week.

+4
1

,

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

    NSString * string = [self.array objectAtIndex:indexPath.row]


    //CALCULATE text SIZE:
    CGSize textSize = [string sizeWithAttributes:NULL];
    return textSize;
}
0

All Articles