开发者

Table cell last line being cutoff when resizing using NSString sizeWithFont:constrainedToSize:lineBreakMode:

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];
    CGFlo开发者_开发百科at 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?


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.


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).


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜