从这里http://www.cnblogs.com/liandwufan/p/4516956.html?utm_source=tuicool 转载过来的

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//

static NSString * cellid = @"PayMentTableViewCell";

PayMentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if( !cell )

{

cell = [[[NSBundle mainBundle] loadNibNamed:cellid owner:self options:nil] lastObject];

}

[cell refresh:[eventArray objectAtIndex:indexPath.row]];

[cell setNeedsUpdateConstraints];

[cell updateConstraintsIfNeeded];

return cell;

}

#pragma mark -

#pragma mark table view delegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 判断indexPath对应cell的重用标示符,

static NSString *reuseIdentifier =@"PayMentTableViewCell";

// 从cell字典中取出重用标示符对应的cell。如果没有,就创建一个新的然后存储在字典里面。

// 警告:不要调用table view的dequeueReusableCellWithIdentifier:方法,因为这会导致cell被创建了但是又未曾被tableView:cellForRowAtIndexPath:方法返回,会造成内存泄露!

PayMentTableViewCell *cell = [offscreenCells objectForKey:reuseIdentifier];

if (!cell)

{

cell = [[[NSBundle mainBundle] loadNibNamed:reuseIdentifier owner:self options:nil] lastObject];

[offscreenCells setObject:cell forKey:reuseIdentifier];

[cell refresh:[eventArray objectAtIndex:indexPath.row]];

}

// 用indexPath对应的数据内容来配置cell,例如:

// cell.textLabel.text = someTextForThisCell;

// ...

// 确保cell的布局约束被设置好了,因为它可能刚刚才被创建好。

// 使用下面两行代码,前提是假设你已经在cell的updateConstraints方法中设置好了约束:

[cell setNeedsUpdateConstraints];

[cell updateConstraintsIfNeeded];

// 将cell的宽度设置为和tableView的宽度一样宽。

// 这点很重要。

// 如果cell的高度取决于table view的宽度(例如,多行的UILabel通过单词换行等方式),

// 那么这使得对于不同宽度的table view,我们都可以基于其宽度而得到cell的正确高度。

// 但是,我们不需要在-[tableView:cellForRowAtIndexPath]方法中做相同的处理,

// 因为,cell被用到table view中时,这是自动完成的。

// 也要注意,一些情况下,cell的最终宽度可能不等于table view的宽度。

// 例如当table view的右边显示了section index的时候,必须要减去这个宽度。

cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

// 触发cell的布局过程,会基于布局约束计算所有视图的frame。

// (注意,你必须要在cell的-[layoutSubviews]方法中给多行的UILabel设置好preferredMaxLayoutWidth值;

// 或者在下面2行代码前手动设置!)

[cell setNeedsLayout];

[cell layoutIfNeeded];

// 得到cell的contentView需要的真实高度

CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

// 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。

height += 1.0f;

return height;

}

// 注意:除非行高极端变化并且你已经明显的觉察到了滚动时滚动条的“跳跃”现象,你才需要实现此方法;否则,直接用tableView的estimatedRowHeight属性即可。

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 以必需的最小计算量,返回一个实际高度数量级之内的估算行高。

// 例如:

return 200;

}

//在自定义UITableViewCell 类中 添加函数

- (void)layoutSubviews

{

[super layoutSubviews];

_remarkLab.preferredMaxLayoutWidth = self.contentView.frame.size.width - (10+40+2+34+(25+15));

_placeLab.preferredMaxLayoutWidth = _remarkLab.preferredMaxLayoutWidth;

[super layoutSubviews];

}

preferredMaxLayoutWidth的值网上的方法都是不对的,我这里用了自己的方法来实现。

以上是第一种方法,我的系统版本是 8.1   上面的是可以正常显示的,低于8的系统版本没有尝试

////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// /////////

这种方法同样在ios8系统里面是OK的,在heightForRowAtIndexPath  里面有一个8以及8以下的区分 需要注意,

#import "MainViewController.h"

#import "TableViewCellA.h"

#import "QuoteTableViewCell.h"

#define SYSTEM_VERSION                              ([[UIDevice currentDevice] systemVersion])

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([SYSTEM_VERSION compare:v options:NSNumericSearch] != NSOrderedAscending)

#define IS_IOS8_OR_ABOVE                            (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

{

TableViewCellA * reduseCell;

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MainViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma UITableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 10;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

TableViewCellA * cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellA"];

if( !cell )

{

cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCellA" owner:self options:nil]lastObject];

}

[cell refreshCell:@"-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath-(UITableViewCell*)tableView:(UITableView *)tableView cellForRow"];

return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

//if( IS_IOS8_OR_ABOVE )

{

return UITableViewAutomaticDimension;

}

/*

if (!reduseCell)

{

reduseCell = [self.tableView dequeueReusableCellWithIdentifier:@"TableViewCellA"];

}

[reduseCell refreshCell:@"-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath-(UITableViewCell*)tableView:(UITableView *)tableView cellForRow"];

[reduseCell updateConstraintsIfNeeded];

[reduseCell layoutIfNeeded];

return [reduseCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

*/

}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewAutomaticDimension;

}

@end

#import "TableViewCellA.h"

@interface TableViewCellA()

@property (weak, nonatomic) IBOutlet UILabel *textLab;

@end

@implementation TableViewCellA

- (void)awakeFromNib {

// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

-(void)layoutSubviews

{

[super layoutSubviews];

[self.contentView updateConstraintsIfNeeded];

[self.contentView layoutIfNeeded];

self.textLab.preferredMaxLayoutWidth = self.textLab.frame.size.width;

}

-(void)refreshCell:(NSString*)str

{

self.textLab.text = str;

}

@end

下面是另外一种方法,

AutoLayout UITableViewCell 动态高度的更多相关文章

  1. iOS开发——使用Autolayout生成动态高度的TableViewCell单元格

    步骤一.TableViewCell中使用Autolayout 要点:Cell的高度必须在Constraints中指明,但不能定死,需要让内部由内容决定高度的View决定动态高度. 如UILabel设置 ...

  2. UITableViewcell autolayout下动态高度

    项目中最经常使用的一个UI就是UITableView了.iOS7.8进一步优化了复用机制,用起来相当爽.配合Autolayout,适配工作减轻了非常多. 曾经做适配工作都是在heightForRow里 ...

  3. AutoLayout深入浅出五[UITableView动态高度]

    本文转载至 http://grayluo.github.io//WeiFocusIo/autolayout/2015/02/01/autolayout5/ 我们经常会遇到UITableViewCell ...

  4. 使用Autolayout xib实现动态高度的TableViewCell

    http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...

  5. AutoLayout处理UITableView动态高度

    我们经常会遇到UITableViewCell的高度要跟随内容而调整,在未引入AutoLayout之前,我们使用以下方法计算Label高度,然后heightForRowAtIndexPath中返回计算的 ...

  6. UITableViewCell动态AutoLayout布局

    相关链接: 使用Autolayout实现UITableView的Cell动态布局和高度动态改变 IOS tableView cell动态高度 (autoLayout) AutoLayoutClub 使 ...

  7. swift 版本 UItableViewCell的动态高度补足

    用swift的朋友们很多都是从ios8开发了, 其中针对table cell高度自动计算的 UITableViewAutomaticDimension 异常好用,但好像只对uilabel对象有效    ...

  8. iOS-动态调整UITableViewCell的高度

    OS-动态调整UITableViewCell的高度iOS开发文档, by 友盟翻译组 stefaliu. 大概你第一眼看来,动态调整高度是一件不容易的事情,而且打算解决它的第一个想法往往是不正确的.在 ...

  9. soui中,列表控件动态高度的使用注意

    1.listview的模板template中,需要增加defHeight属性,即默认高度,同时,不能出现itemHeight属性,否则动态高度会失效 2.数据适配器中,重写getViewDesired ...

随机推荐

  1. hdu 4599 Dice 概率DP

    思路: 1.求f[n];dp[i]表示i个连续相同时的期望 则 dp[0]=1+dp[1]     dp[1]=1+(5dp[1]+dp[2])/6     ……     dp[i]=1+(5dp[1 ...

  2. java打印时间精确到毫秒

    package net.floodlightcontroller.conflict; import java.io.*; import java.util.*; import java.text.Si ...

  3. CSDN代码片排版

    CSDN上发博文,尤其是有代码的,要用好“插入代码”功能(见下图).这是必须的,未来的IT程序员,需要用这种方式,展示出自己的代码.这是尊重读者,树立品牌的需要. 相关方法,参考见视频<发布第一 ...

  4. AE数据加载

    1. 数据加载问题: 任何系统都离不开数据的加载,下边就AE中几种常用的数据加载做一个列举.以便查阅: 1.加载个人数据库 个人数据库是保存在Access中的数据库.其加载方式有两种:通过名字和通过属 ...

  5. iOS Container View Controller

    一.UIViewController 做iOS开发的经常会和UIViewController打交道,从类名可知UIViewController属于MVC模型中的C(Controller),说的更具体点 ...

  6. 5、处理模型数据ModelAndView、Map、Model以及@SessionAttributes注解

    Spring MVC提供了以下几种途径输出模型数据 —— ModelAndView: 处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据.数据会添加到request域中. ...

  7. Linux使用者管理(1)---用户账号

    linux很重要的应用就是作为服务器的操作系统.服务器的作用是给多用户提供各种“服务”(可能是读服务器上的文件,或者是利用服务器进行数值计算)那么如果多用户共同拥有一台服务器,就需要对服务器上的用户进 ...

  8. 素数筛法--SPOJ Problem 2 Prime Generator

    质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单, ...

  9. asp.net TreeView与XML配合使用v1.1

    刚我在做Tree view 绑定时自己摸索了一下,网上有人说TreeView绑定数据源,用什么递归绑定啥的,我不想看了,就自己试着写了一个 我是这样做的,如果有什么问题请大神指导,我是菜鸟额.. 1: ...

  10. IS_ERR、PTR_ERR、ERR_PTR

    最近在使用filp_open打开文件时遇到到一个问题,当打开一个并不存在的文件时,filp_open返回值值为0xfffffffe,而并不是0(NULL),这是因为内核对返回指针的函数做了特殊处理.内 ...