iOS8上自动布局:两个多行(或许0线)标签 [英] ios8 autolayout: BOTH multi-line(maybe 0 line) label

查看:83
本文介绍了iOS8上自动布局:两个多行(或许0线)标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两个UILabels(标题,副标题)在单元格中显示想。结果
作为标题:多行的> 0结果
字幕:0 多行的 2搜索结果
这里的bug (例如,标题和放大器;字幕均为2系,细胞的ind​​exPath.row = 1):结果
我第一次运行模拟器,标题只显示1行和字幕显示2行。我的滚动和的tableView后回到第一个indexPath.row,它正确显示!这似乎是这样的:搜索结果

I wanna display two UILabels(Title, Subtitle) in a cell.
for Title: multiple line > 0
for Subtitle: 0 multiple line 2

A bug here(say, title&subtitle are both 2 lines, cell's indexPath.row = 1):
The first time I run the simulator, title only shows 1 line and subtitle shows 2 lines. After I scrolled the tableView and back to the first indexPath.row, it display correctly! It seems like this:

首播时间:
结果
-----------------结果
这是瓦,并且它应该显示...搜索结果
这是字幕,并且它应该显示结果
2行。
的结果
-----------------搜索结果
滚动后,回到这个单元:结果
-----------------结果
这是标题,它应该显示结果
两行。搜索结果
这是字幕,并且它应该显示结果
2行。
的结果
-----------------

First time:
—————————————————
This is the tile, and it should display...

This is the subtitle, and it should show
2 rows.

—————————————————

After scroll and back to this cell:
—————————————————
This is the title, and it should display
two rows.

This is the subtitle, and it should show
2 rows.

—————————————————

我所做的:结果
在控制器:结果 tableView.estimatdRowHeight = 79 结果 tableView.rowHeight = UITableViewAutomaticDimension 结果
结果
在故事板:结果
标题:结果

What I've done:
In controller:
tableView.estimatdRowHeight = 79
tableView.rowHeight = UITableViewAutomaticDimension

In storyboard:
For title:


  1. 前导空间,上海华

  2. 尾随空间,上海华

  3. 顶部空间的SuperView

  4. 对齐导致字幕

  5. 对齐尾随到字幕

  6. 底部空间字幕搜索

有关副标题:结果


  1. 底部空间,上海华

  2. 对齐通往标题

  3. 对齐后,以标题

  4. 顶部空间标题

我糊涂了这个bug数天,任何想法解决这个?非常感谢!(对不起,缺乏形象因为我没有得到足够的声誉><)

I'm confused for this bug several days, any ideas to solve this? Big thanks!!!(Sorry for lacking image cuz I don't get enough reputation ><)

推荐答案

细胞不正确的初始宽度开始了。

Cells that are loaded from a storyboard don't start out with the right initial width.

这就是为什么标签不是尺寸不正确的第一次,但正确显示,一旦你(重新加载数据,或)滚动细胞关闭屏幕,然后在屏幕上。

This is why the label isn't sized properly the first time, but appears correctly once you (reload the data, or) scroll the cell off-screen, then on-screen.

由于单元宽度为最初不正确,标签结束使用 preferredMaxLayoutWidth 这比表更宽。 (标签认为它有更多的空间来适合一行一切。)

Since the cell width is initially incorrect, the label ends up using a preferredMaxLayoutWidth which is wider than the table. (The label thought it had more room to fit everything on one line.)

这为我工作的解决方案是,以确保我(子类)细胞的宽度相匹配的的tableView的宽度:

The solution which worked for me was to make sure my (subclassed) cell's width matched the tableView's width:

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

    [cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];

    [self configureCell:cell forRowAtIndexPath:indexPath];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
}

在TableViewCell.m:

In TableViewCell.m:

- (void)adjustSizeToMatchWidth:(CGFloat)width
{
    // Workaround for visible cells not laid out properly since their layout was
    // based on a different (initial) width from the tableView.

    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;

    // Workaround for initial cell height less than auto layout required height.

    rect = self.contentView.bounds;
    rect.size.height = 99999.0;
    rect.size.width = 99999.0;
    self.contentView.bounds = rect;
}

我也建议检查出smileyborg的 href=\"http://stackoverflow.com/a/18746930/4151918\">出色答卷,连同他的sample code 。这就是我放倒关闭的解决方案,当我撞到你有同样的问题。

I'd also recommend checking out smileyborg's excellent answer about self-sizing cells, along with his sample code. It's what tipped me off to the solution, when I bumped into the same issue you are having.

这篇关于iOS8上自动布局:两个多行(或许0线)标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆