1

I have a UILabel in a UICollectionViewCell in a class named CollectionViewCell its all working. But the problem comes when the UILabel is smaller than the text I want to put in.

I've already made the [cell.label sizeToFit]; in the cellForItemAtIndexPath.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   NSDictionary *news = [self.select objectAtIndex:indexPath.row];

   CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

   cell.label.text = [news objectForKey:@"content"];
   [cell.label setFont:[UIFont systemFontOfSize:15]];
   cell.label.numberOfLines = 0;
   [cell.label sizeToFit];
   cell.label.preferredMaxLayoutWidth = 292.f;
   return cell;
}

Already made the constraints like this answers https://stackoverflow.com/a/16009707/2577738 and it works only with the 7 first cells, the others just have a height of 29, here's the code that I implemented to make size of the cell variable.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    int h = 29 + cell.label.frame.size.height;
    return CGSizeMake(292, h);
}

this is the result in some it workedenter image description here

Community
  • 1
  • 1
ElioMB
  • 189
  • 1
  • 1
  • 13
  • Have you tried this line of code [cell.label sizeToFit]; after dequeue of cell of collectionview? – Surjeet Singh Nov 06 '14 at 19:03
  • @Dev i've put the cell at row at index path in the question – ElioMB Nov 06 '14 at 19:20
  • No, it's sizeForItemAtIndexPath which return size for cell. Can you post - (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath method from your code. It must be in your code. – Surjeet Singh Nov 06 '14 at 19:24

1 Answers1

0

If this is with a UICollectionViewFlowLayout, you must implement collectionView:layout:sizeForItemAtIndexPath:. That is where you must do your measurement.

The modern technique is to use auto layout constraints and call systemLayoutSizeFittingSize: to work out the cell size for you. Here's actual code:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    self.modelCell.lab.text = self.sectionData[indexPath.section][indexPath.row]
    var sz = self.modelCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    sz.width = ceil(sz.width); sz.height = ceil(sz.height)
    return sz
}

Note that this means you must supply the content of the label in both collectionView:layout:sizeForItemAtIndexPath: (to do the measurement) and in cellForItemAtIndexPath: (to make the label contents appear in the actual cell).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And for the complete example, see https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/ViewController.swift – matt Nov 06 '14 at 19:34
  • i've tried setting the label.text in the collectionView:layout:sizeForItemAtIndexPath: too, but with no result, i add an image to the question to be more clear – ElioMB Nov 06 '14 at 22:04
  • But download the project I pointed you to. It is about using different sized labels and it works. – matt Nov 07 '14 at 00:24