I have created an UICollectionView cell and it's displaying perfectly, but my doubt is have given an constant size of cell in sizeForItemAtIndexPath. My issue is each and every cell is containing dynamic height, how can i calculate and give at sizeForItemAtIndexPath
Asked
Active
Viewed 344 times
0
vikingosegundo
- 52,040
- 14
- 137
- 178
Harish
- 2,496
- 4
- 24
- 48
-
See http://stackoverflow.com/a/31604233/312594 – par Sep 02 '15 at 18:51
1 Answers
0
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.
Mike M
- 4,879
- 5
- 38
- 58