tags:

views:

32

answers:

1

I need to adjust numberOfLines of label in cellForRowAtIndexPath. Label is in word wrap mode. However, the actual calculation of label's height is taking place in heightForRowAtIndexPath.

How to calculate numberOfLines in cellForRowAtIndexPath? For now I'm just using some very big number.

+1  A: 

From the Apple Docs:

To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.

Then you may use this technique in the heightForRowAtIndexPath method to determine how tall the cell should be.

Constants:

static const CGFloat kCellFontSize = 14.0;
static const CGFloat kCellWidth = 300.0;
static const CGFloat kCellHeightMax = 999.0;
static const CGFloat kCellPadding = 10.0;

Method:

CGSize maxSize = CGSizeMake( kCellWidth, kCellHeightMax );

CGSize labelSize = 
        [[self cellTextString] sizeWithFont: [self cellFont] 
                                constrainedToSize: maxSize 
                                    lineBreakMode: UILineBreakModeTailTruncation];

return (labelSize.height + (2 * kCellPadding));
jessecurry
I also use this technique, but the question is `numberOfLines` which I guess better to set during cell construction in `cellForRowAtIndexPath`.
Michael
I apologize. I usually set the number of lines to 0, which will cause the label to use as many lines as needed.
jessecurry