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

Ube*_*son 7 iphone uitableview ios ios8

使用iOS 8.3模拟器和Xcode 6.3.2.

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

UITabel约束在UITableViewCell中

然后我使用an estimatedRowHeight并设置rowHeightUITableViewAutomaticDimension(我在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
Run Code Online (Sandbox Code Playgroud)

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

UITableView看起来很好

但是当我在故事板中添加附件视图时,不更改任何代码:

配件视图

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

巨大的细胞

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

有谁知道为什么配件视图如此糟糕地混乱,我能做些什么来解决它?有关示例项目,请访问http://github.com/UberJason/SelfSizingNightmare.

Joh*_*tin 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;
}
Run Code Online (Sandbox Code Playgroud)

另外,打电话[tableView reloadData]似乎viewDidAppear也能解决这个问题。