根据文本量更改UITableViewCell高度

Jos*_*ane 56 iphone objective-c uitableview ios

我需要能够在UITableView中调整单个单元格的高度,以使其适合其详细标签中的文本量.

我玩了以下但是它对我不起作用:

如何在没有自定义单元格的情况下在UITableViewCell中包装文本

希望你能帮忙,谢谢.

编辑:

试图代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
Run Code Online (Sandbox Code Playgroud)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    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 + 20;
}
Run Code Online (Sandbox Code Playgroud)

这没有用,它显示了单元格上的整个字符串,但单元格高度根本不受影响.

ste*_*k21 81

它很简单,只需将其添加到您的代码中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

它会自动计算行的高度而不是返回浮点数... :-)

希望这可以帮助!

  • 仅适用于标准单元格...不适用于我的自定义单元格. (12认同)
  • 它正在运行,但只能使用基本的"cell.textLabel"或Apple的其他人.使用一些自定义单元会更难... (5认同)
  • 只有在函数estimatedHeightForRowAtIndexPath中提供了估计值时,这才能完美.如果您有一个imageview并且它设置为宽高比适合,这将无法正常工作 (4认同)
  • 对我而言,它适用于我的自定义单元格,只需添加您想要的约束即可. (3认同)

AJP*_*tel 56

嗨Josh,

使用tableView:heightForRowAtIndexPath:您可以在运行时给出每行的大小.现在你的问题是如何从你的字符串中获取高度有这个代码的NSString类中的函数你的问题,

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *str = [dataSourceArray objectAtIndex:indexPath.row];
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"%f",size.height);
    return size.height + 10;
}
Run Code Online (Sandbox Code Playgroud)

通过下面的线你设置你的标签的数量.线到最大 所以在cellForRowAtIndexPath:方法中设置它.

cell.textLabel.numberOfLines = 0;

如果你使用一些自定义单元格,然后使用此管理所有标签的字符串并获得所有高度的总和,然后设置单元格的高度.

编辑:从 iOS 8开始,如果您为标签设置了正确的自动布局约束,那么您必须仅设置以下委托方法来实现此目的.

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
   //minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;    
   return UITableViewAutomaticDimension; 
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

就是这样.无需任何计算.有关更多信息,请查看本教程.

  • `sizeWithFont` [已被弃用](http://stackoverflow.com/a/18897897/2446155) (3认同)

Pha*_*inh 45

在你的CustomCell:记住为你的顶部和底部添加约束UILabel

对于调整UILabel高度取决于文本只需将UILabel行更改为0(请参阅此处的答案)

然后在你的代码中,只设置2行

self.tableView.estimatedRowHeight = 80;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)

这是我的自定义单元格
在此输入图像描述 这是我的UILabel约束
在此输入图像描述
你将实现的屏幕

在此输入图像描述

=== 建议 ===
如果你的手机有一些UILabelsImages(不像我的例子)那么:

  • 你应该把所有UILabelsImages一个GroupView(查看)
  • 添加constraint top and bottom to supper view为此GroupView(就像我的图像中的UILabel)
  • 调整UILabel高度就像我上面的建议一样
  • 调整GroupView高度取决于内容(内容全部UILabelsImages)
  • 最后,改变estimateRowHeighttableView.rowHeight喜欢上面的代码

希望这有帮助


Nit*_*bur 21

根据您提供的代码,我认为您只增加单元格高度而不是cell.textLabel的高度.

理想情况下,您应该设置cell.textLabel的框架大小和单元格,以便查看单元格中的完整文本.

一个简洁的方法来查看视图在大小方面的错误,是将它与背景颜色不同(尝试将cell.textLabel背景设置为黄色)并查看高度是否实际设置.

这是它应该如何

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];

    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = cell.textLabel.font;
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    cell.textlabel.frame.size = labelSize; 
    cell.text = cellText;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

更新:这是一个很老的答案,这个答案中的许多行可能会被弃用.

  • ```UILineBreakModeWordWrap```在iOS 6.0中已弃用,请改用````NSLineBreakByWordWrapping```` (4认同)

mum*_*umu 7

对于快速的开发人员:

自定义单元格:首先,您可以计算文本的高度,如下所示:

func calculateHeight(inString:String) -> CGFloat
    {
        let messageString = inString
        let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 15.0)]

        let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)

        let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 222.0, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)

        let requredSize:CGRect = rect
        return requredSize.height
    }
Run Code Online (Sandbox Code Playgroud)

设置宽度您的文本标签

然后调用此函数:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        heightOfRow = self.calculateHeight(inString: conversations[indexPath.row].description)

        return (heightOfRow + 60.0)
}
Run Code Online (Sandbox Code Playgroud)

对于基本单元格:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return UITableViewAutomaticDimension
    }
Run Code Online (Sandbox Code Playgroud)

此功能不适用于自定义单元格.

希望它会奏效.


归档时间:

查看次数:

88290 次

最近记录:

6 年,3 月 前