当文本比平时长时,heightForRowAtIndexPath永远不会显示多行。

这是我的代码:

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat     result = 44.0f;
    CGFloat     width = 0;
    CGFloat     tableViewWidth;
    CGRect      bounds = [UIScreen mainScreen].bounds;

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
        tableViewWidth = bounds.size.width;
    else
        tableViewWidth = bounds.size.height;

    width = tableViewWidth - 110;       // fudge factor, 115 isn't quite right

    NSUInteger index = [indexPath row];
    id doc = [[self displayedObjects] objectAtIndex:index];

    NSString *title = [doc title];

    if (title)
    {
        // The notes can be of any height
        // This needs to work for both portrait and landscape orientations.
        // Calls to the table view to get the current cell and the rect for the
        // current row are recursive and call back this method.
        CGSize      textSize = { width, 20000.0f };     // width and height of text area
        CGSize      size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

        size.height += 29.0f;               // top and bottom margin
        result = MAX(size.height, 44.0f);   // at least one row
    }

    return result;
}

任何帮助将不胜感激。
谢谢!

最佳答案

要使标签显示多行文本,必须将其numberOfLines属性设置为允许的最大行数(对于任意行数,则设置为0)。因此,在cellForRowAtIndexPath:方法中,在设置单元格时添加:

...
cell.textLabel.numberOfLines = 0;
...

10-08 07:45