开发者

Cant set the height of my uitableviewcell right

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 te开发者_运维问答xt http://img34.imageshack.us/img34/214/bildschirmfoto20100120uw.png


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];


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜