Sajib Ghosh
Sajib Ghosh

Reputation: 418

How to change UICollectionview Cell Height dynamically?

I have a UICollectionView and a custom UIcollectionViewCell.
Inside that cell there is one UITableView.

The content of the tableview changes dynamically so it's height also changes dynamically.
I want UICollectionViewCell to change it's height with respect to the tableview inside the Collectionviewcell.

How to do that?

Upvotes: 0

Views: 1318

Answers (1)

Bharat Modi
Bharat Modi

Reputation: 4180

Use collectionView's delegate for setting size to cell,

Objective-C

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

      NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);

      //Calculate the height required for tableView inside the cell and set it to the cell
      return CGSizeMake(80, 80);
}

Swift

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

      NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);

      //Calculate the height required for tableView inside the cell and set it to the cell
      return CGSizeMake(80, 80);
}

Hope it helps you.

Upvotes: 1

Related Questions