简体   繁体   English

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

[英]iOS 8 Self Sizing Cells With Accessory View

Using iOS 8.3 simulator and Xcode 6.3.2. 使用iOS 8.3模拟器和Xcode 6.3.2。

I'm using the self sizing cells technique in iOS 8, and it works terrifically when the cell has no accessory view, but breaks when the cell has an accessory view. 我在iOS 8中使用自我调整大小的技术,当单元格没有附件视图时它很有效,但当单元格有附件视图时会中断。 I set constraints on the label inside the cell: 我在单元格内的标签上设置了约束:

UITabel约束在UITableViewCell中

Then I use an estimatedRowHeight and set the rowHeight to UITableViewAutomaticDimension (I'm omitting tableView:numberOfRowsInSection: here, but it just returns 3 for this example): 然后我使用一个estimatedRowHeight并将rowHeight设置为UITableViewAutomaticDimension (我省略了tableView:numberOfRowsInSection: here,但是这个例子只返回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

And bam, everything looks great: 而且,一切看起来都很棒:

UITableView看起来很好

But when I add an accessory view in the Storyboard, without changing any of the code: 但是当我在故事板中添加附件视图时,不更改任何代码:

配件视图

the first cell goes to hell. 第一个细胞下地狱。 Xcode's view debugger tells me the label height is 1785 points, and I had to use a gif to show it here: Xcode的视图调试器告诉我标签高度是1785点,我必须使用gif在这里显示它:

巨大的细胞

I do notice that the other two cells are sized fine. 我注意到其他两个细胞的大小都很好。 I also note that when I rotate the simulator, the first cell gets resized properly, including after it's rotated back to portrait. 我还注意到,当我旋转模拟器时,第一个单元格会正确调整大小,包括在它旋转回肖像后。

Does anyone know why the accessory view messes this up so badly, and what I can do to fix it? 有谁知道为什么配件视图如此糟糕地混乱,我能做些什么来解决它? A sample project is available at http://github.com/UberJason/SelfSizingNightmare . 有关示例项目,请访问http://github.com/UberJason/SelfSizingNightmare

This is a bug indeed. 这确实是一个错误。 I have sent your project to Apple. 我已将您的项目发送给Apple。 There is a reply from Apple. 有一个Apple的回复。

在此输入图像描述

It seems to be caused by the label inside the cell having numberOfLines = 0 . 它似乎是由具有numberOfLines = 0的单元格内的标签引起的。

Although I'm not sure why, it looks as though adding the accessory to the cell leaves auto layout unable to calculate the height of the label when the table first loads. 虽然我不确定为什么,看起来好像在单元格中添加附件会使自动布局无法在桌面首次加载时计算标签的高度。

I was able to fix your example by adding a preferred max width to the label - which seems to be enough to let the layout system figure out the height in time: 我能够通过在标签上添加一个首选的最大宽度来修复你的例子 - 这似乎足以让布局系统及时计算出高度:

- (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;
}

Alternatively, calling [tableView reloadData] in viewDidAppear also seems to fix it. 或者,在viewDidAppear调用[tableView reloadData]似乎也解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM