具有附件视图的iOS 8自定义单元格

时间:2015-06-25 17:24:07

标签: ios iphone uitableview ios8

使用iOS 8.3模拟器和Xcode 6.3.2。

我正在iOS 8中使用自我调整大小的技术,当单元格没有附件视图时它会非常有效,但是当单元格有附件视图时会中断。我在单元格内的标签上设置了约束:

UILabel constraints in a UITableViewCell

然后我使用estimatedRowHeight并将rowHeight设置为UITableViewAutomaticDimension(我在此省略tableView:numberOfRowsInSection:,但此示例只返回3):

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

...

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

    cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";

    return cell;
}

@end

而且,一切看起来都很棒:

UITableView looks fine

但是当我在Storyboard中添加一个附件视图时,没有更改任何代码:

With accessory view

第一个细胞下地狱。 Xcode的视图调试器告诉我标签高度是1785点,我必须使用gif在这里显示:

Huge cell

我注意到其他两个细胞的大小都很好。我还注意到,当我旋转模拟器时,第一个单元格会正确调整大小,包括在它旋转回肖像后。

有谁知道为什么配件视图会如此糟糕,以及我可以做些什么来修复它?示例项目位于http://github.com/UberJason/SelfSizingNightmare

2 个答案:

答案 0 :(得分:2)

这确实是一个错误。我已将您的项目发送给Apple。有一个Apple的回复。

enter image description here

答案 1 :(得分:0)

这似乎是由具有numberOfLines = 0的单元格内的标签引起的。

虽然我不确定为什么,看起来好像在单元格中添加附件会使自动布局无法在桌面首次加载时计算标签的高度。

我能够通过在标签上添加首选最大宽度来修复您的示例 - 这似乎足以让布局系统及时计算出高度:

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

    // Give the layout system something to help it estimate the label height    
    cell.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width;
    cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";

    return cell;
}

或者,在[tableView reloadData]中调用viewDidAppear似乎也可以解决问题。

相关问题