首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >heightForRowAtIndexPath不显示多行

heightForRowAtIndexPath不显示多行
EN

Stack Overflow用户
提问于 2011-03-06 02:15:29
回答 2查看 848关注 0票数 1

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

下面是我的代码:

代码语言:javascript
复制
#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;
}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-06 02:20:07

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

代码语言:javascript
复制
...
cell.textLabel.numberOfLines = 0;
...
票数 3
EN

Stack Overflow用户

发布于 2011-03-06 02:28:27

我想您不会使用自定义的UITableViewCell。因此,您可能正在使用类似于以下代码的内容来设置标题:

代码语言:javascript
复制
cell.textLabel.text = @"long long long text";

要使文本显示在多行上,您必须配置默认的标题UILabel,使其文本显示在多行上。如下所示:

代码语言:javascript
复制
cell.textLabel.numberOfLines = 3;

计算行数的方法与计算单元格高度的方法相同。事实上,我认为你必须计算两者才能做你想做的事情(高度适应内容)。

代码语言:javascript
复制
CGSize      size = [title sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

textSize中,重要的是设置width属性的行宽和一个“大”到足以“承载”文本的高度值。

初始化size后,可以将高度除以UILabel行的高度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5205770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档