【问题标题】:iOS 7: Get height for default UITableViewCell?iOS 7:获取默认 UITableViewCell 的高度?
【发布时间】:2014-02-01 20:49:53
【问题描述】:

我使用的是默认的 UITableViewCell,只是它的 textLabel。我的文字是多行的。计算其高度的最佳方法是什么?

我知道有多种 NSString 大小调整方法,但为了使用这些方法,您必须指定宽度。而且我不知道默认 textLabel 的宽度,我怀疑它会根据放置在其中的文本而改变。

我也尝试过使用这里描述的方法:

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

...但它不起作用(估计大小总是返回 0);该帖子暗示该解决方案仅适用于 UITableViewCell 子类。 (我可以子类化,但这不是必需的。)

建议?我的应用是特定于 iOS 7 的。

谢谢!

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    UITableView rowHeight 属性。如果您没有明确设置,UITableView 会将其设置为标准值。

    【讨论】:

    • 我认为他正在覆盖heightForRowAtIndexPath: 来设置高度,但是当遵循示例项目并调用CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 时,使用标准UITableViewCell 时高度为0。当我尝试时,我也发现了同样的事情。
    【解决方案2】:

    我让它与标准 UITableViewCell 一起使用 - 在您列出的问题中使用 github,但替换这些函数。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
        }
    
        // Configure the cell for this indexPath
        //[cell updateFonts];
        NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
        cell.textLabel.text =[dataSourceItem valueForKey:@"body"];
    
        return  cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
        NSString *cellText = [dataSourceItem valueForKey:@"body"];
    
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    
        return labelSize.height;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (self.isInsertingRow) {
            // A constraint exception will be thrown if the estimated row height for an inserted row is greater
            // than the actual height for that row. In order to work around this, we return the actual height
            // for the the row when inserting into the table view.
            // See: https://github.com/caoimghgin/TableViewCellWithAutoLayout/issues/6
            return [self tableView:tableView heightForRowAtIndexPath:indexPath];
        } else {
            return 500.0f;
        }
    }
    

    哦,还要删除自定义单元类的寄存器,所以我们得到一个 UITableViewCell 而不是 RJTableViewCell。另外我认为这里有这个(即使它是 UITableViewCell)dequeueReusableCellWithIdentifier 永远不会返回 nil,我们也不会正确设置我们的单元格。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //[self.tableView registerClass:[RJTableViewCell class] forCellReuseIdentifier:CellIdentifier];
    
        ...
    }
    

    这里基本上遵循了这个例子 - https://stackoverflow.com/questions/129502/how-do-i-wrap-text-in-a-uitableviewcell-without-a-custom-cell。我认为关键是不要像对单元格进行子类化时那样询问单元格的高度,而是根据文本和字体来计算它。您不能要求单元格的高度这一事实对我来说似乎有点奇怪,这让我觉得@Jeffery Thomas 可能是对的,从长远来看,创建一个自定义单元格可能更安全。我猜可能取决于你的项目。

    【讨论】:

    • 您假设宽度为 280。这是一个有效的假设吗? (iOS 7 上的标签有多宽?)
    • 我不是 100% 的,这个数字来自链接的示例。通过检查应该很容易验证。如果你真的很偏执,你可以通过编程检查第一个单元格的宽度并将其缓存到其他单元格,因为在这个例子中它总是相同的。
    • 我担心的是 textLabel 的宽度可能会根据内容动态变化,所以即使我明确检查它我也不知道它的最宽。我们知道它保持不变吗?
    • 这取决于您是否同时支持横向和纵向。你只会发短信吗?或者有图吗?总是有图像吗?...如果您的单元格很简单,总是文本或总是文本和图标,那么我认为这是一个好方法。如果复杂且多变,那么在您的情况下,自定义 UITableViewCell 会更好吗?也许有更好的方法,但如果有我不知道。
    • 我认为这种方法在 iPhone 6 和 6 plus 发布之前就可以使用,当时所有的 iPhone 都是 320 点宽。现在已经不是这种情况了。
    【解决方案3】:

    你需要继承UITableViewCell

    您向UITableViewCell 提出的要求比它承诺提供的要多。这是麻烦的秘诀。

    创建一个子类并构建一个原型。你会知道所有的限制,所以这很容易。

    【讨论】:

    • 将文本放入标准 UITableViewCell 超出了它的承诺范围?
    • @GregMaletic 将多行文本放入标准 UITableViewCell 超出了它的承诺范围。
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多