iOS动态高度UITableViewCell和heightForRowAtIndexPath [英] iOS dynamical height of UITableViewCell and heightForRowAtIndexPath

查看:545
本文介绍了iOS动态高度UITableViewCell和heightForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个大型项目中使用Autolayout作为我的新UITableViewCells。

I'm using Autolayout for my new UITableViewCells in a large project.

我有一个TableView,其中每行的高度是自动计算的,我不知道不要使用委托函数 heightForRowAtIndexPath

I've one TableView where the height of each row is calculated automatically, there I don't use the delegate function heightForRowAtIndexPath.

我已经宣布估计行高:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

我的问题是:在另一个TableViewController上有很多UITableViewCells,我在编程上需要在 heightForRowAtIndexPath 。我知道将所有单元格转换为使用唯一解决方案会更好,但在这个项目中有很多不同的单元格,所以我想使用一种解决方法并将动态计算的高度与自动布局和程序计算的行相结合高度。

My question is: on another TableViewController there are a lot of UITableViewCells, where I programmatically need to declare the height of the cell in heightForRowAtIndexPath. I know that It would be better to convert all cell's to use a unique solution, but in this project are a lot of different cell's, so I'd like to use a workaround and combine the dynamically calculated height with autolayout and the programmatically calculated row height.

这可能吗?

推荐答案

如果您正在使用iOS 8及更高版本,您无需动态计算高度。自动布局将为您完成所有工作。但是如果你使用低于IOS 8,你需要计算单元格高度。

If you are using iOS 8 and above, you do not need to calculate height dynamically. Auto layout will do all for you. But if you are using lower than IOS 8, you need to calculate cell height.

对于IOS 8:

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

并在控制器中添加以下代码:

And add below code in your controller:

tableView.estimatedRowHeight = 400.0
tableView.rowHeight = UITableViewAutomaticDimension

其中 estimatedRowHeight 应该是您的单元格的最大高度。

Where estimatedRowHeight should be max height which can be for your cell.

谢谢

这篇关于iOS动态高度UITableViewCell和heightForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆