1

I want to dynamicaly set the cell's rowheight to the UILabel's height (which is cell's subview).

The difficulty is UILabel needs to get its height by checking its string content

Two problems:

1- How can I set correctly the numberofLines in UILabel depending on the string lenght?

2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?

here is some code, whcih I am sure I do somethinsg wrong..

Nsstring st= "very long text...";
CGSize theSize = [st sizeWithFont:font constrainedToSize:CGSizeMake(250.0f, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGRect cg = CGRectMake(0.0f, 0.0f, 250.0f, theSize.height); 
UILabel *label=[[UILabel alloc] initWithFrame:cg]; 
label.textAlignment=UITextAlignmentLeft;
label.text=st;
[label setNumberOfLines:10];// this is trouble!! how to set this!?
[cell addSubview:label];
Spring
  • 11,333
  • 29
  • 116
  • 185
  • `UILabel`'s [numberOfLines](http://developer.apple.com/library/iOS/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/numberOfLines) is the *maximum* number of lines. "To remove any maximum limit, and use as many lines as needed, set the value of this property to 0." – albertamg Aug 18 '11 at 16:41
  • @albertamg Amazing! when I set to zero most of my problems solved! now how can set the row.height to theSize.height? tnx – Spring Aug 18 '11 at 18:40
  • you need to implement `heightForRowAtIndexPath:` and return the appropriate height for the row. You can move the computation of `theSize` to a helper method and use it from `cellForRowAtIndexPath:` too. – albertamg Aug 18 '11 at 19:14
  • @albertamg tnx if you can answer this, I will accept – Spring Aug 19 '11 at 08:11
  • @albertamg also where should i send the string content? while in init method? it should be somewhere before heightForRowAtIndexPath called – Spring Aug 19 '11 at 08:21
  • I followed your advise and answered the question :) – albertamg Aug 19 '11 at 10:18
  • where does the string data come from? Is it part of your Model? You normally use the indexPath argument to pull the cell content from the Model – albertamg Aug 19 '11 at 10:41
  • The same problem with a simple solution :) http://stackoverflow.com/a/13815244/1551062 – xeravim Dec 11 '12 at 07:27

3 Answers3

3
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {

CGSize labelSize = CGSizeMake(200.0, 20.0);
if ([string length] > 0)
labelSize = [string sizeWithFont: [UIFont boldSystemFontOfSize: 17.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
return 24.0 + labelSize.height;
}

You need to implement heightForRowAtIndexPath. In my sample code, I returned 24 + the height of the label

Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
1

You can have a separate textForRow: function that you can both use to set the text of the label, and also to calculate the size of the label (and the cell) in the delegate method (much like you have already done)

thelaws
  • 7,991
  • 6
  • 35
  • 54
1

(Mashup of my comments)

1- How can I set correctly the numberofLines in UILabel depending on the string lenght?

UILabel's numberOfLines is the maximum number of lines. "To remove any maximum limit, and use as many lines as needed, set the value of this property to 0."

2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?

You need to implement heightForRowAtIndexPath: and return the appropriate height for the row. You can move the computation of theSize to a helper method and use it from cellForRowAtIndexPath: too.

albertamg
  • 28,492
  • 6
  • 64
  • 71