简体   繁体   English

iOS自动版式:在这种情况下,如何正确计算像元高度?

[英]iOS Auto-Layout: How should I calculate cell height correctly in this scenario?

As you can see in this picture 正如您在这张图片中看到的 在此处输入图片说明

, the imageView 's size is fixed and I want to have the cell's subviews "exert pressure" and make the content view expand to fit them. imageView的大小是固定的,我想让单元格的子视图“施加压力”并使内容视图扩展以适合它们。 Let the intrinsic content size of these subviews drive the height of the table view cell's content view. 让这些子视图的固有内容大小驱动表格视图单元格的内容视图的高度。 It's obvious that the cell height depends on the maximum height of messageButton and imageView : 很明显,细胞高度取决于最大高度messageButtonimageView

How should I add those constraints? 我应该如何添加这些约束?

maybe you can try this: 也许您可以尝试以下方法:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

but before that, you should use Auto-layout to add constraints correctly, you don't need think about the imageView and the button height, just add the constraints of distance to the bottom and top. 但是在此之前,您应该使用“自动布局”正确添加约束,您无需考虑imageView和按钮高度,只需将距离的约束添加到底部和顶部即可。 Good luck! 祝好运!

In Swift 3 Swift 3中

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension

}

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

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