1

I get the object from CoreData like this self.name = [[abc anyObject] valueForKey:@"name"] and display it:

cell.nameLabel.text = [NSString stringWithFormat:@"%@",self.name];
cell.nameLabel.numberOfLines = 0;
cell.nameLabel.lineBreakMode = NSLineBreakByWordWrapping;

How can I get the correct height for my cell to return it in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
davel
  • 43
  • 6

3 Answers3

2

This is the solution:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize constraintSize = CGSizeMake(286.0f, CGFLOAT_MAX);
    UIFont *theFont  = [UIFont systemFontOfSize:14.0f];
    CGSize theSize;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        CGRect frame = [[self.mArray objectAtIndex:indexPath.row] boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:theFont} context:nil];
    theSize = frame.size;
    }
    else
    {
        theSize = [[self.mArray objectAtIndex:indexPath.row] sizeWithFont:theFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    }

    return theSize.height;
    }

Also in - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I used cell.label.numberOfLines = 0

If you are using more than one Label in row - in CGSizeMake(286.0f, CGFLOAT_MAX) use lower value than 286.0f (for example 150.0f)

davel
  • 43
  • 6
1

Just implement a static method in your UITableViewCell class that is able to calculate cell height basing on provided string and a width. Cell should know how it is constructed and how much space it will occupy.

For example:

+ (CGFloat)heightForWidth:(CGFloat)width text:(NSString *)text {

    CGFloat height = ... ;

    return height;
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
verges
  • 21
  • 2
  • I didn't understand it. I Have my own class for cell, and how I get 'CGFloat height = '? – davel May 28 '14 at 15:32
  • You accidentally the whole interesting part. – Matthias Bauch May 28 '14 at 17:28
  • If your cell contains only one multiline label this method would looke sth like this (just use the same label font and line breaking mode that you use for your label:`+ (CGFloat)cellHeightForTitle:(NSString *)title width:(CGFloat)width { CGFloat height = 0.0; height += 2 * verticalMargin; height += [title sizeWithFont:font constrainedToSize:CGSizeMake(width - 2*sideMargin, MAXFLOAT) lineBreakMode:lineBreakMode].height; return height; }` – verges May 29 '14 at 12:02
0

Try to disable auto layout in your storyboard and then call sizeToFit.

cell.nameLabel.text = [NSString stringWithFormat:@"%@",self.name];
cell.nameLabel.numberOfLines = 0;
cell.nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.nameLabel sizeToFit];
diogo.appDev
  • 1,595
  • 5
  • 16
  • 30