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);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(154, 154);
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:

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)

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