11

i'm currently developing on an app, which displays some tweets in a tableview. On the storyboard i created a prototype-cell, which includes the basic gui concept of a tweet entry.

It looks circa like this:

++++++++++++++
++Username++++
++++++++++++++
++Tweet+++++++
++++++++++++++
++Time-Ago++++
++++++++++++++

Now i'm calculating the height of the cell with the following code, but somehow it fails.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * currentTweet = [tweetArray objectAtIndex: indexPath.row];
    NSString * tweetTextString = [currentTweet objectForKey: @"text"];
    CGSize textSize = [tweetTextString sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(630, 1000) lineBreakMode: NSLineBreakByWordWrapping];

    float heightToAdd = 24 + textSize.height + 15 + 45;
    if(heightToAdd < 90) {
        heightToAdd = 90;
    }

    return heightToAdd;
}

By the way, there is something other, which is strange. If i scroll the tableview the whole app seems to freeze. Is this normal or am i doing something wrong?

Lukas
  • 1,346
  • 7
  • 24
  • 49
  • It is not normal @ freezing. From outside, I have a feeling, this could be due to one of the two reasons: 1) you are doing too many calculations due to which it is leading to this. 2) In order to display text in a formatted way, apparently you might be using some kind of encoding on each of row's text. – Reno Jones Feb 12 '13 at 20:30
  • Hello, thank's for your comment. You're right. I called [tableView reloadData] every second to update the seconds ago counter. I removed the counter and the freezing stopped. I'm trying to do this inside the cellview. :) – Lukas Feb 12 '13 at 20:37
  • 1
    Do a foreach on tableView.visibleCells and update each of them with the current time. Avoid reloadData at all costs, as you found out. :) – escrafford Feb 12 '13 at 20:42
  • The approach you're using for calculating variable height text cells is the same as I've done before with success - that's about as fast as I've managed to make it before. – escrafford Feb 12 '13 at 20:44
  • Hi guy's, thank's for your help. I played around a little bit and got it to work finnaly! Thanks :) – Lukas Feb 12 '13 at 20:45

2 Answers2

13

Try this for your issue:

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

    NSDictionary * currentTweet = [tweetArray objectAtIndex: indexPath.row];

    NSString * tweetTextString = [currentTweet objectForKey: @"text"];

    CGSize textSize = [tweetTextString sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(240, 20000) lineBreakMode: UILineBreakModeWordWrap]; //Assuming your width is 240

    float heightToAdd = MIN(textSize.height, 100.0f); //Some fix height is returned if height is small or change it to MAX(textSize.height, 150.0f); // whatever best fits for you

    return heightToAdd;
}

Hope it helps.

Reno Jones
  • 1,979
  • 1
  • 18
  • 30
3

If you are looking for the iOS 7 answer I ran across it here:

iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize

Community
  • 1
  • 1
Rick Roberts
  • 885
  • 11
  • 13