0

I set the size of my UITableCell´s with this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString * vergleich = [nachricht objectAtIndex:indexPath.row];
    CGSize size = [vergleich sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]
                     constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
                         lineBreakMode:UILineBreakModeWordWrap];
    return size.height + 30;
}

nachricht is a NSArray which contains all the messages. The code just looks how long the message (with a specified font) is and calculate the height. I set + 30, because over the message(UITextView) is a UIlabel.

The UITextView, which should contain the messages, get the size with this code:

- (void)setTweetText:(NSString *)_tweet;{
    CGSize size = [_tweet sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]
                  constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
                      lineBreakMode:UILineBreakModeWordWrap];
    [textText setFrame:CGRectMake(55, 25, 268, size.height + 10)];
    textText.text = _tweet;
    [textText sizeToFit];
    textText.dataDetectorTypes = UIDataDetectorTypeLink;
}

Now there is a problem and I don't know why: The UITextView is bigger then the cell, even if I set the size of the cell height there is a unpleasant distance between the TextView and the next cell. Why doesn't he get the right height for some cells. Here is an example:

alt text http://img34.imageshack.us/img34/214/bildschirmfoto20100120uw.png

3 Answers 3

1

All I can say is in my cellForRowAtIndexPath I use the following and it works. My heightForRowAtIndexPath is nearly identical to yours.

double d = [self tableView:table heightForRowAtIndexPath:indexPath];
UILabel* label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 280, d-20)] autorelease];
label.numberOfLines = 100;
label.lineBreakMode = UILineBreakModeWordWrap;
[label setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
label.text  = [descriptions objectAtIndex:indexPath.section];
[cell.contentView addSubview:label];
0

one possibility is you have set the CGSize width to be different in each function, for row height you have CGSizeMake(268, MAX_HEIGHT) and for setTweetText it is CGSizeMake(262, MAX_HEIGHT)

4
  • Oh sry, I set it just for trying smaller - That is not the problem.
    – Flocked
    Jan 20, 2010 at 23:11
  • so what was the constraining size you used in both functions when you took the picture? Jan 20, 2010 at 23:48
  • 268 is the right size - I set it to smaller to see, if he just add space for one more line, so the cell is bigger. But he didn´t make it right.
    – Flocked
    Jan 20, 2010 at 23:58
  • the way you have it above though would make the textview taller than the row height you are calculating, did you get this same screenshot when you had both set at 268? Jan 21, 2010 at 0:03
0

its not a good idea to sizeWithFont from that method, its fairly heavy and will chop up your scrolling performance. if possible calculate the heights before and store them for quick access from that method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.