views:

622

answers:

3

Hello, 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

A: 

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)

John Stallings
Oh sry, I set it just for trying smaller - That is not the problem.
Flocked
so what was the constraining size you used in both functions when you took the picture?
John Stallings
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
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?
John Stallings
+1  A: 

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];
David Kanarek
A: 

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

drunknbass