如何获得UITableViewCell的动态高度 [英] How to get dynamically height for UITableViewCell

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

问题描述

我创建了Customized tableViewCell,我在UITableViewCell中添加了UIView SubView。所以我的动态文字& UIView中的图像内容,它将根据文本和图像大小而变化。
但现在在HeightforRowAtIndex中得到错误,这里我直接返回UIView frame.size.height + 10;它在iOS8.1中工作正常,但它不能在所有版本下工作。

I created Customized tableViewCell, I added UIView SubView in UITableViewCell. So my all dynamic Text & Image Content in UIView and it will change according to text and image size. but now getting error in HeightforRowAtIndex , here i returned direct UIView frame.size.height+10; it working fine in iOS8.1 but it will not work below all version.

下面添加了一段代码&带图像。

Here below added piece of code & with image.

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    _cellbackground=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 310, 0)];
    _cellbackground.backgroundColor=[UIColor whiteColor];
    [cell addSubview:_cellbackground];
return cell;
}

这里_cellbackground视图,高度将根据文本和图像内容动态变化。

Here _cellbackground view, height will change dynamically according to text and image content.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return   _cellbackground.frame.size.height+10;
}

这里我根据UIView高度返回高度,但它只能工作( iOS 6s)iOS8.1。但其他人则无法正常工作。
请建议我获得身高的最佳方法在此输入图片说明此图片来自iPhone 5(7.1),它将无法正常工作。

here I return height according to UIView height, but it will working only on (iPhone 6s)iOS8.1. but others it will not work properly. Please suggest me best way to get height enter image description here this image from iPhone 5(7.1) , it will not work properly.

在这里输入图像描述它来自iPhone 6(8.1)正常工作。

enter image description here it is from iPhone 6(8.1) working properly.

请指导我返回高度的最佳解决方案。(HeightForRowAtIndex) 。

Please guide me best solution to return height.( HeightForRowAtIndex).

推荐答案

用于在viewDidLoad中设置的UITableViewCell的动态高度

for dynamically height for UITableViewCell set in viewDidLoad

_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;

根据内容高度设置自动单元格高度

this set automatic cell height as per content height

如果有效,请告诉我......

Let me know if this works...

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

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