1

When using the following code to re-size a table row the last line of text is always cutoff, no matter how many lines there are. But there is white space added that looks like enough space for the text.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat restOfTheCellHeight = tableView.rowHeight - cell.detailTextLabel.frame.size.height;
    CGSize constrainedSize = CGSizeMake(cell.detailTextLabel.frame.size.width, CGFLOAT_MAX);
    CGSize textHeight = [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize:constrainedSize lineBreakMode:cell.detailTextLabel.lineBreakMode];
    CGFloat newCellHeight = (textHeight.height + restOfTheCellHeight);
    if (tableView.rowHeight > newCellHeight) {
        newCellHeight = tableView.rowHeight;
    }
    return newCellHeight;
}

Here is the code in cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCellTableRowTypeSingleLineValueSmallLabel *cell = (CustomCellTableRowTypeSingleLineValueSmallLabel *)[tableView dequeueReusableCellWithIdentifier:@"CellTypeMultiLineLabelInCellSmallCell"];

    if (cell == nil) {
        NSArray *xibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellTableRowTypeSingleLine" owner:nil options:nil];
        for(id currentObject in xibObjects) {
            if([currentObject isKindOfClass:[CustomCellTableRowTypeSingleLineValueSmallLabel class]]){
                cell = (CustomCellTableRowTypeSingleLineValueSmallLabel *)currentObject;
            }
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }   

    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.detailTextLabel.numberOfLines = 0;

    cell.detailTextLabel.text = self.attributeStringValue;
    cell.textLabel.text = self.rowLabel;

    return cell;
}

Any ideas?

oojoe
  • 133
  • 1
  • 12
  • Are you resizing your detailTextLabel correctly? Post what you have in cellForRowAtIndexPath please. – MishieMoo Aug 18 '11 at 18:15
  • Hi @MishieMoo, I'm not setting the height of detailTextLabel explicitly. But rather setting the numberOfLines to 0 so that it can grow with the following: `cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; cell.detailTextLabel.numberOfLines = 0;` – oojoe Aug 18 '11 at 18:54
  • I added the full code to the original post. Thanks. – oojoe Aug 18 '11 at 19:01

3 Answers3

0

You need to call [cell.detailTextLabel sizeToFit] in order for the label to actually resize in cellForRowAtIndexPath. It will not resize on its own just because you set numberOfLines to 0. See this question and read its answers for more clarification.

Community
  • 1
  • 1
MishieMoo
  • 6,620
  • 2
  • 25
  • 35
  • Thanks for the feedback, but trying those suggestions actually cuts off everything after the first line. I don't think it's a problem with the label size, as if I manually set the size of the table cell height to something larger I do see all of the text. – oojoe Aug 18 '11 at 21:21
  • Is your label only one line high in your nib? – MishieMoo Aug 19 '11 at 15:11
  • In my nib the label is one line high as sometimes that is how I want it to render. But it is over ridden in cellForRowAtIndexPath. I've figured it out (see my answer), thanks for your help! – oojoe Aug 19 '11 at 18:59
0

You are calculating the cell height appropriately in your heightForRowAtIndexPAth method, but then in your cellForRowAtIndexPath method you are never actually using it to set the height of your label within it.

So the table is allocating the right amount of space based on your heightForRowAtIndexPath, but then inserting into that space the unresized cell that you return from cellForRowAtIndexPath. I think this might the the cause of the problem and would explain the results you are seeing.

In cellForRowAtIndexPath you need to actually set the height of the label using the same calculation.

i.e.

 CGSize constrainedSize = CGSizeMake(cell.detailTextLabel.frame.size.width, CGFLOAT_MAX);
 CGRect cframe = cell.detailTextLabel.frame;
 cframe.size.height = constrainedSize.height;
 cell.detailTextLabel.frame = cframe;

You may also need to actually set the content view frame as well (not sure how it works with a non-custom cell).

I'm also not sure its a good idea to be calling cellForRowAtIndexPath from the heightForRowAtIndexPath method (it would probably be better to just directly access the text data you are using for the size calculation directly).

gamozzii
  • 3,911
  • 1
  • 30
  • 34
  • Thanks for the feedback. I tried this and the result is still the same. I don't think it is a problem with the text label size and it is getting re-sized. If I have 9 lines of text it's only the 10th line that doesn't show up. Also is I manually set the height of the table cell I can see all of the text. – oojoe Aug 19 '11 at 13:19
0

Turns out I just needed to enable all of the Autosizing options in interface builder for the label.

oojoe
  • 133
  • 1
  • 12