So I have a cell in UICollectionView
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell: myCell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! myCell
// cell.descrption is an UILabel
cell.description.text = newText
return cell
}
and
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.bounds.width*0.95, collectionView.bounds.height/4)
}
My question is:
newText
is acquired from the web. So it's dynamically changed. After the cell's text is set to newText, how should I adjust the label so that it has the right size to fit "newText" and how should I adjust the cell's size and display it on the screen?
I noticed that sizeForItemAtIndexPath()
is invoked before cellForItemAtIndexPath()
. Therefore, I can't calculate the size for nextText within sizeForItemAtIndexPath()
.
Its a great way to check the what text size would be in
sizeForItemAtIndexPath()
, as you already know thatsizeForItemAtIndexPath()
is invoked beforecellForItemAtIndexPath()
you can have a dummy cell having same information. e.g you may refer to my code which i used inUITableView
and implement according to your requirement.you can do same to achieve size in
UICollectionView
as both share same inheritance. so there would be no problem. Even you can return custom height depending upon the number of characters in your text. like