在iOS 8中的单个UITableViewCell上有2个多行标签

时间:2022-09-20 15:54:26

I have a scenario in which a single UITableViewCell have 2 UILable. I can have long text in it coming from server which causes both labels to be multiline at same time.

我有一个场景,其中一个UITableViewCell有2 UILable。我可以从服务器中获取长文本,这会使两个标签同时成为多行。

I have implemented autolayout and

我已经实现了autolayout和

label.numberofline = 0;
tableVieww.estimatedRowHeight  = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;

but it results only one label to expand not both. Please help.

但它只会导致一个标签扩展而不是两者兼而有之。请帮忙。

Thanks in advance

提前致谢

4 个解决方案

#1


0  

What You have so far looks fine. That should work without the need calculate the label height as some suggested.

你到目前为止看起来很好。这应该工作,而不需要像一些人建议的那样计算标签高度。

Just make sure that you have the proper constraints on your labels.

只需确保您的标签上有适当的约束。

  1. Top constraint between the top label and its superview.
  2. *标签与其超级视图之间的最高约束。
  3. Bottom constraint between the bottom label and its superview.
  4. 底部标签与其超级视图之间的底部约束。
  5. A vertical spacing constraint between the two labels.
  6. 两个标签之间的垂直间距约束。

I tested it. it's working!

我测试了它。它的工作原理!

- (void)viewDidLoad {
    [super viewDidLoad];

    dynamicTableVuew.rowHeight = UITableViewAutomaticDimension;
    dynamicTableVuew.estimatedRowHeight = 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];

    UILabel *label1 = (UILabel *)[cell viewWithTag:1];
    UILabel *label2 = (UILabel *)[cell viewWithTag:2];

    label1.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
    label2.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";

    return cell;
}

That's all I have in the view controller. and nothing special in storyboard.

这就是我在视图控制器中所拥有的一切。故事板中没什么特别的。

One last thing to check. Make sure that your tableview is connected to your view controller with an outlet. You may see the outlet code there but does not mean its connected. maybe it was removed by mistake. the following lines will not take effect:

最后要检查一下。确保您的tableview通过插座连接到视图控制器。您可能会看到那里的插座代码,但并不意味着它已连接。也许它被误删了。以下行不会生效:

tableVieww.estimatedRowHeight  = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;

在iOS 8中的单个UITableViewCell上有2个多行标签

#2


3  

try this:

尝试这个:

use this code to calculate row height

使用此代码计算行高

Note : ExtraHeight"H"

注意:ExtraHeight“H”

Suppose you have cell with height 100 and in that cell a label has height 20 than extra height will be 80, because this code is calculating the height of label as per text with its size.

假设您有高度为100的单元格,并且在该单元格中,标签的高度为20,而额外高度为80,因为此代码根据文本的大小计算标签的高度。

you are going to put this code in heightforRowatindexpath so cell.label can't get there so just pass the text and use Method-2

你将把这个代码放在heightforRowatindexpath中,所以cell.label无法到达那里所以只需传递文本并使用Method-2

Method - 1:

方法 - 1:

-(CGFloat)setRowHeightWithLabel:(UILabel *)lbl withLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
   CGFloat totalHeight;
   CGFloat lblWidth = lbl.frame.size.width;

   CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

   if (lblTextSize.height > lbl.frame.size.height) {
    totalHeight = (lblTextSize.height+H);
   }
   else
   {
       totalHeight = lbl.frame.size.height+H;
   }

   NSLog(@"+++++++++++++Height - %.2f",totalHeight);

   return totalHeight;
}

Use this for use in heightforRow method

使用它在heightforRow方法中使用

Method - 2:

方法-2:

-(CGFloat)setRowHeightWithLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
  {
     CGFloat totalHeight;
     CGFloat lblWidth = 300;

     CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

    if (lblTextSize.height > 20) {
        totalHeight = (lblTextSize.height+H);
    }
    else
    {
        totalHeight = 20+H;
    }

    NSLog(@"+++++++++++++Height - %.2f",totalHeight);

    return totalHeight;
}

#3


1  

I think you are using autolayout I had the same problem I resolved it by setting maximum width of labels and the in label's size Inspector you should set preferred width to your max width

我认为你正在使用自动布局我遇到了同样的问题我通过设置标签的最大宽度和标签的大小Inspector来解决它你应该将首选宽度设置为你的最大宽度

在iOS 8中的单个UITableViewCell上有2个多行标签

#4


-2  

If you want two labels to be multi-line (supposing you are using "Subtitle" cell type"), you must set numberOfLines to 0 to both labels, eg:

如果您希望两个标签是多行的(假设您使用“Subtitle”单元格类型“),则必须将numberOfLines设置为0以指向两个标签,例如:

UITableViewCell cell = [UITableViewCell alloc]init];
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;

Your lines about cell height seems to be correct

关于细胞高度的线条似乎是正确的

#1


0  

What You have so far looks fine. That should work without the need calculate the label height as some suggested.

你到目前为止看起来很好。这应该工作,而不需要像一些人建议的那样计算标签高度。

Just make sure that you have the proper constraints on your labels.

只需确保您的标签上有适当的约束。

  1. Top constraint between the top label and its superview.
  2. *标签与其超级视图之间的最高约束。
  3. Bottom constraint between the bottom label and its superview.
  4. 底部标签与其超级视图之间的底部约束。
  5. A vertical spacing constraint between the two labels.
  6. 两个标签之间的垂直间距约束。

I tested it. it's working!

我测试了它。它的工作原理!

- (void)viewDidLoad {
    [super viewDidLoad];

    dynamicTableVuew.rowHeight = UITableViewAutomaticDimension;
    dynamicTableVuew.estimatedRowHeight = 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];

    UILabel *label1 = (UILabel *)[cell viewWithTag:1];
    UILabel *label2 = (UILabel *)[cell viewWithTag:2];

    label1.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
    label2.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";

    return cell;
}

That's all I have in the view controller. and nothing special in storyboard.

这就是我在视图控制器中所拥有的一切。故事板中没什么特别的。

One last thing to check. Make sure that your tableview is connected to your view controller with an outlet. You may see the outlet code there but does not mean its connected. maybe it was removed by mistake. the following lines will not take effect:

最后要检查一下。确保您的tableview通过插座连接到视图控制器。您可能会看到那里的插座代码,但并不意味着它已连接。也许它被误删了。以下行不会生效:

tableVieww.estimatedRowHeight  = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;

在iOS 8中的单个UITableViewCell上有2个多行标签

#2


3  

try this:

尝试这个:

use this code to calculate row height

使用此代码计算行高

Note : ExtraHeight"H"

注意:ExtraHeight“H”

Suppose you have cell with height 100 and in that cell a label has height 20 than extra height will be 80, because this code is calculating the height of label as per text with its size.

假设您有高度为100的单元格,并且在该单元格中,标签的高度为20,而额外高度为80,因为此代码根据文本的大小计算标签的高度。

you are going to put this code in heightforRowatindexpath so cell.label can't get there so just pass the text and use Method-2

你将把这个代码放在heightforRowatindexpath中,所以cell.label无法到达那里所以只需传递文本并使用Method-2

Method - 1:

方法 - 1:

-(CGFloat)setRowHeightWithLabel:(UILabel *)lbl withLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
   CGFloat totalHeight;
   CGFloat lblWidth = lbl.frame.size.width;

   CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

   if (lblTextSize.height > lbl.frame.size.height) {
    totalHeight = (lblTextSize.height+H);
   }
   else
   {
       totalHeight = lbl.frame.size.height+H;
   }

   NSLog(@"+++++++++++++Height - %.2f",totalHeight);

   return totalHeight;
}

Use this for use in heightforRow method

使用它在heightforRow方法中使用

Method - 2:

方法-2:

-(CGFloat)setRowHeightWithLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
  {
     CGFloat totalHeight;
     CGFloat lblWidth = 300;

     CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

    if (lblTextSize.height > 20) {
        totalHeight = (lblTextSize.height+H);
    }
    else
    {
        totalHeight = 20+H;
    }

    NSLog(@"+++++++++++++Height - %.2f",totalHeight);

    return totalHeight;
}

#3


1  

I think you are using autolayout I had the same problem I resolved it by setting maximum width of labels and the in label's size Inspector you should set preferred width to your max width

我认为你正在使用自动布局我遇到了同样的问题我通过设置标签的最大宽度和标签的大小Inspector来解决它你应该将首选宽度设置为你的最大宽度

在iOS 8中的单个UITableViewCell上有2个多行标签

#4


-2  

If you want two labels to be multi-line (supposing you are using "Subtitle" cell type"), you must set numberOfLines to 0 to both labels, eg:

如果您希望两个标签是多行的(假设您使用“Subtitle”单元格类型“),则必须将numberOfLines设置为0以指向两个标签,例如:

UITableViewCell cell = [UITableViewCell alloc]init];
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;

Your lines about cell height seems to be correct

关于细胞高度的线条似乎是正确的