【问题标题】:UITableView with auto height in iOS 8iOS 8 中具有自动高度的 UITableView
【发布时间】:2015-05-21 12:08:54
【问题描述】:

在 iOS 8(和它的 SDK)之前,我使用这个委托方法做了 UITableViewCell 的自动高度:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (!_prototypeCell)
    {
        _prototypeCell = [tableView dequeueReusableCellWithIdentifier:@"MyShowIdentifier"];
    }

    EventShow *show = self.myShows[indexPath.row];

    self.prototypeCell.categoryNameLabel.text = show.event.category.name;
    self.prototypeCell.eventNameLabel.text = show.event.title;

    [self.prototypeCell layoutIfNeeded];
    CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    return size.height + 1.0f;
}

我的单元格有两个标签,一个在另一个之上,带有自动布局约束,告诉标签可以垂直增长。一切正常。

iOS 8 发布后,我仅在第一次使用方法 systemLayoutSizeFittingSize: IS EXECUTED 时收到“无法同时满足约束”错误。委托方法被调用 3 次。这是错误的详细信息:

(
    "<NSLayoutConstraint:0x7fd1e8ebce30 H:|-(5)-[UILabel:0x7fd1eb0e00a0'Cine']   (Names: '|':UITableViewCellContentView:0x7fd1eb0c0210 )>",
    "<NSLayoutConstraint:0x7fd1eb06b120 H:[UILabel:0x7fd1eb0e00a0'Cine']-(5)-|   (Names: '|':UITableViewCellContentView:0x7fd1eb0c0210 )>",
    "<NSLayoutConstraint:0x7fd1e8e7be50 'UIView-Encapsulated-Layout-Height' H:[UITableViewCellContentView:0x7fd1eb0c0210(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fd1eb06b120 H:[UILabel:0x7fd1eb0e00a0'Cine']-(5)-|   (Names: '|':UITableViewCellContentView:0x7fd1eb0c0210 )>

那里有一个名为“UIView-Encapsulated-Layout-Height”的新约束设置为零。我现在不知道那是什么,但我确定它应该 > 0。

自动高度在 iOS 8 中仍然有效,但它会引发我无法修复的错误。

有什么想法吗?非常感谢!

【问题讨论】:

    标签: uitableview ios8 autolayout height


    【解决方案1】:

    您得到的错误是标签的宽度,因为您设置了特定的宽度,并且您还设置了前导和尾随约束。所以这里发生的是标签打破前导,尾随或宽度约束以满足标签的尾随约束。

    但是为了满足尾随的约束,它将打破宽度约束。

    您需要在这里做的只是为两个标签设置前导、顶部、尾随和底部约束。此外,您需要设置这些标签之间的垂直间距,并且无需为两个标签设置高度和宽度限制。

    您还可以在属性检查器中设置行 = 0,换行符 = 换行。

    您还需要设置“self.tableView.rowHeight = UITableViewAutomaticDimension;”在 viewDidLoad 中。

    【讨论】:

    • 哇,很抱歉,我复制了错误的错误(已编辑)。而不是 UIView-Encapsulated-Layout-Width 它是 UIView-Encapsulated-Layout-Height。答案仍然适用?再次感谢和抱歉。
    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多