iOS8:可以使用“tableview.rowHeight = UITableViewAutomaticDimension”对于静电电池? [英] iOS8: Possible to use "tableview.rowHeight = UITableViewAutomaticDimension" for static cells?

查看:418
本文介绍了iOS8:可以使用“tableview.rowHeight = UITableViewAutomaticDimension”对于静电电池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView有五个静态单元格。我需要一个单元格的单元格高度自动调整其内容,这是一个UILabel。

I have a UITableView that has five static cells. I need the cell height of one cell to adjust automatically to its contents, which is one UILabel.

我有什么方法可以使用..

Is there any way I can use..

tableView.estimatedRowHeight = 42.0
tableView.rowHeight = UITableViewAutomaticDimension

..对于带有静态单元格的表格视图,或者这仅适用于具有动态原型单元格的表格视图?

..for a table view with static cells, or does this only work for table views with dynamic prototype cells?

(或者你有什么其他方法可以推荐吗?)

(Or is there any other way of doing this that you would recommend?)

UITableView是在嵌入到容器视图中的TableViewController中。

The UITableView is in a TableViewController which is embedded into a Container View.

五个静态单元格彼此完全不同,仅用于一个应用程序场景,所以我不这样做看到动态原型和自定义单元子类的重点。

The five static cells are quite different from one another and are only used in one app scene, so I don't see much point in going for dynamic prototypes and custom cell subclasses.

我像这样更新tableview

I update the tableview like this

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_myTableView reloadData];
}

我向UILabel添加约束,如下所述:
http://www.appcoda.com/self-sizing-cells/

I add constraints to the UILabel as described here: http://www.appcoda.com/self-sizing-cells/

推荐答案

对于iOS 8,我发现你不能采用与动态表视图单元相同的策略。

For iOS 8, I found out that you cannot do the same strategy as for Dynamic Table View Cell.

为了自动调整静态单元格的高度,我实现了这两个UITableViewDelegate方法:

To automatically adjust the height of static cells, I implement these two UITableViewDelegate methods:

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

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

希望有所帮助。

这篇关于iOS8:可以使用“tableview.rowHeight = UITableViewAutomaticDimension”对于静电电池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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