首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有附件视图的iOS 8自调整大小单元格

带有附件视图的iOS 8自调整大小单元格
EN

Stack Overflow用户
提问于 2015-06-26 01:24:07
回答 2查看 510关注 0票数 7

使用iOS 8.3模拟器和Xcode6.3.2。

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

然后我使用estimatedRowHeight并将rowHeight设置为UITableViewAutomaticDimension (我在这里省略了tableView:numberOfRowsInSection:,但在本例中它只返回3):

代码语言:javascript
复制
@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

然后,一切看起来都很棒:

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

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

我确实注意到其他两个单元格的大小都很好。我还注意到,当我旋转模拟器时,第一个单元格的大小会适当地调整,包括在它旋转回纵向之后。

有人知道为什么附件视图会把事情搞得这么糟吗?我能做些什么来修复它呢?在http://github.com/UberJason/SelfSizingNightmare上提供了一个示例项目。

EN

回答 2

Stack Overflow用户

发布于 2015-07-12 21:10:49

这确实是一个bug。我已经把你的项目发给苹果了。这是来自苹果的回复。

票数 2
EN

Stack Overflow用户

发布于 2015-07-09 21:18:27

这似乎是由细胞内带有numberOfLines = 0的标签引起的。

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

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

代码语言:javascript
复制
- (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;
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31056840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档