2

I have created a UITableViewCell with a NIB file. There is 1 label in it which is going to contain a tweet. So it needs to be a dynamic height. There also is a timeAgo label that has to fit underneath the tweet label.

I'm trying stuff with frames en sizes but I can't get the perfect solution.. I do this in the UITableViewCell file in the drawrect method.

self.tweet.lineBreakMode = UILineBreakModeWordWrap;
self.tweet.numberOfLines = 0;
self.tweet.font = [UIFont fontWithName:@"Arial" size:13.0f];
[self.tweet sizeToFit];  

CGFloat tweetHeight = self.tweet.frame.size.height;

self.timeAgo.lineBreakMode = UILineBreakModeWordWrap;
self.timeAgo.numberOfLines = 0;
self.timeAgo.font = [UIFont fontWithName:@"Arial" size:11.0f];
[self.timeAgo sizeToFit];

CGFloat timeAgoHeight = self.timeAgo.frame.size.height;

self.timeAgo.frame = CGRectMake(88, tweetHeight, 100, timeAgoHeight + 10.0f);

I have also tried a stringhelper which I found in a tutorial.

The:

- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {

My HeightForRow methods is also already different because I use different cell styles. At the moment I return a hard value for each cell style but that also needs to change to the cellheight.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Rolf Koenders
  • 439
  • 3
  • 8

2 Answers2

7

See this tutorial, http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

The trick is to make the label grow with the size of the cell, than you can just set the size of the cell and the cell will grow with it.

Set the timeAgo label to align it self to the bottom of the cell.

Set the numberOfLines of tweet to 0 via IB,re move all the draw code and only implement the following:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item  = [self.item objectAtIndex:indexpath.row];

    CGFloat height = 85.0f;

    if ([item isKindOfClass:[Tweet class]]) {
        Tweet *tweet = (Tweet *)item;
        CGSize titleSize = [tweet.tweet sizeWithFont:[UIFont fontWithName:@"Arial" size:13.0f] constrainedToSize:CGSizeMake(260.0f, MAXFLOAT)];

        // adde the 24 pixels to get the height plus the time ago label.
        height =  titleSize.height + 24.0f;

    } else if( [item isKinfOfClass:[SC_Release class]]) {
        height = 65.0f;
    }

   return height;
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Hmm, its not that good atm =p http://cl.ly/1Y00411b0F0N0h2R2G2z / http://cl.ly/3t0g3L1r1k3h0M163m2i – Rolf Koenders Sep 02 '11 at 09:39
  • Thanks for the edit! But the problem is the label wich contain the tweet text, its not resizing or something. http://cl.ly/2t2Q2P0p3g2v0Y0Y2d2u – Rolf Koenders Sep 02 '11 at 09:52
  • Well it is, but I think you will need to increase the 24 pixel to match the y offset because of the username. Also make sure that the cell is at least the height of the image. Also check that the 16.0f in the CGSize make is width of you label (260.0f is just an example). – rckoenes Sep 02 '11 at 09:57
  • Thanks its working fine after i added: self.tweet.lineBreakMode = UILineBreakModeWordWrap; self.tweet.numberOfLines = 0; [self.tweet sizeToFit]; to the drawrect. Thanks allot! Maby i will see you @ Appsterdam some time ;) – Rolf Koenders Sep 02 '11 at 10:03
  • :D Will definitely be at an Appsterdam meet en drink. – rckoenes Sep 02 '11 at 10:11
3
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [tweetsArray indexPath.row];
    CGSize labelSize = [string sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] 
                                                     constrainedToSize:CGSizeMake(280.0f, MAXFLOAT) 
                                                         lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 20;
}
Robin
  • 10,011
  • 5
  • 49
  • 75
  • This approach is pretty rad, too. Instead of talking to some other arbitrary array, though, I literally get the cell with `[self tableView:tableView cellForRowAtIndexPath:indexPath]` and pull the text value from the cell's `textLabel.text` property. – Ben Kreeger Oct 10 '12 at 20:54