Autolayout和Storyboard中动态UITableViewCell的高度

首先需要在Storyboard中创建好TableViewController,使用动态Cell,在Prototype Cells中设计好Cell界面。

屏幕快照 2013-12-23 下午5.44.56

接着,定义好Autolayout,注意Autolayout一定要在上下都绑定控件的位置,不要只从上到下定义,只有正确定义Autolayout,后面我们用到的systemLayoutSizeFittingSize方法才会返回正确的结果。

屏幕快照 2013-12-23 下午5.47.41

Xcode会提示Autolayout的各种Ambiguity,提示修改控件的Compression resistance,如下图:

屏幕快照 2013-12-23 下午5.45.07

这里让Xcode智能修正就可以了,具体哪个控件的Compression resistance无所谓,因为我们最终的目的是让UITabelViewCell的高度去适合所有控件的大小。

然后,因为是在Xcode 5 iOS 7模式下设计的Storyboard,所以在iOS 7下运行肯定是没问题的:

屏幕快照 2013-12-23 下午5.47.57

屏幕快照 2013-12-23 下午5.48.30

出现这个问题的原因是:iOS 7和iOS 6中的许多控件默认高度都是不一样的,在其他普通UIView下,有了Autolayout,控件当然会正确显示。但是UITableViewCell的高度是通过UITableView的heightForRowAtIndexPath方法来返回的。默认情况下,它是保持不变的。所以当Cell内控件的高度发生变化后,如果Cell高度没有因此而作出调整,肯定会出问题的。

那么怎样解决问题呢?理想状态下是这样的,在UITableView的cellForRowAtIndexPath方法中创建并返回Cell,然后在heightForRowAtIndexPath方法中计算并返回Cell的高度。

但是真正的执行顺序是相反的,如果在TableView中有10个Row(假设都可以显示在屏幕上的话,这样不存在Cell的重用),iOS会先调用10次heightForRowAtIndexPath,然后再调用10次cellForRowAtIndexPath。也就是说按照iOS的执行顺序,我们要在Cell创建前知道他的高度。

那么是不是可以在heightForRowAtIndexPath中先创建Cell,并返回高度,然后在之后的cellForRowAtIndexPath调用接着使用这个Cell?好主意!不过问题远没有没有想象中简单。如果在heightForRowAtIndexPath调用dequeueReusableCellWithIdentifier:forIndexPath:方法的话,会出现栈溢出问题,类似这样:

屏幕快照 2013-12-23 下午5.55.37

也就是说dequeueReusableCellWithIdentifier:forIndexPath:会反过来调用heightForRowAtIndexPath方法。

还没完,还有一个问题,多次调用dequeueReusableCellWithIdentifier:forIndexPath:方法会产生不同的Cell,即便是IndexPath是一样的,可以做个很简单的示例证明:

// 在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法 内

static NSString *CellIdentifier = @"MyCell" ;

MyCell *cell1 = [tableView dequeueReusableCellWithIdentifier :CellIdentifierforIndexPath :indexPath];

MyCell *cell2 = [tableView dequeueReusableCellWithIdentifier :CellIdentifierforIndexPath :indexPath];

NSLog ( @"%d" , cell1 == cell2);

结果会输出0。cell1不会等于cell2的。

rage

哈哈,冷静。问题很快会解决的,第一个问题,heightForRowAtIndexPath无法使用dequeueReusableCellWithIdentifier:forIndexPath:方法,那么我们可以使用旧的dequeueReusableCellWithIdentifier方法,也就是没有IndexPath参数的,这个是可以使用的,当然使用dequeueReusableCellWithIdentifier的话,我们需要手动判断Cell返回nil的情况。

第二个问题,我们不去YY着dequeueReusableCellWithIdentifier:forIndexPath:会按照IndexPath来返回Cell,自己在heightForRowAtIndexPath方法中提前缓存创建的Cell,Key就是IndexPath,Value是Cell,然后在cellForRowAtIndexPath方法中使用缓存的Cell就OK。

那么,在TableViewController中加入必要的字段:

// 测试数据源

NSMutableArray *_dataSource;

// 缓存 Cell

NSMutableDictionary *_cellCache;

在viewDidLoad中初始化相关数据:

//viewDidLoad 初始化

_dataSource = [ NSMutableArray arrayWithArray : @[ @"Mgen" , @"Tony" ,@"Jerry" , @" 一二三 " ] ];

_cellCache = [ NSMutableDictionary dictionary ];

把创建Cell逻辑写在一个方法内(注意在heightForRowAtIndexPath:indexPath中无法使用dequeueReusableCellWithIdentifier:forIndexPath:方法,所以这里需使用dequeueReusableCellWithIdentifier方法):

- ( MyCell *)getCellFromIndexPath:( NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"MyCell" ;

    // 注意在 heightForRowAtIndexPath:indexPath 无法使用dequeueReusableCellWithIdentifier:forIndexPath:

    MyCell *cell = [ self . tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 用 dequeueReusableCellWithIdentifier: 就得判断 Cell 为 nil 的情况

    if (!cell)

    {

        cell = [[ MyCell alloc ] init ];

    }

    // 这里把数据设置给 Cell

    cell. titleLabel . text = [ _dataSource objectAtIndex :indexPath. row ];

    return cell;

}

在UITableView中执行画龙点睛一笔,使用systemLayoutSizeFittingSize方法来计算创建Cell的高度并返回,如下代码:

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

{

    // 获取 Cell

    MyCell *cell = [ self getCellFromIndexPath :indexPath];

    // 缓存 Cell

    [ _cellCache setObject :cell forKey : @( indexPath. row ) ];

    // 更新 UIView 的 layout 过程和 Autolayout

    [cell setNeedsUpdateConstraints ];

    [cell updateConstraintsIfNeeded ];

    [cell. contentView setNeedsLayout ];

    [cell. contentView layoutIfNeeded ];

    // 通过 systemLayoutSizeFittingSize 返回最低高度

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

    return height;

}

接着在cellForRowAtIndexPath方法内重用缓存的Cell(代码里还有如果没有缓存再次调用创建Cell的逻辑,不过目前觉得没这种可能性,因为heightForRowAtIndexPath方法发生在cellForRowAtIndexPath方法之前):

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

{

    // 获取缓存的 Cell

    MyCell *cachedCell = [ _cellCache objectForKey : @( indexPath. row ) ];

    // 如果没有缓存再次调用 getCellFromIndexPath 来创建 Cell

    if (!cachedCell)

    {

        return [ self getCellFromIndexPath :indexPath];

    }

    return cachedCell;

}

OK,现在Cell无敌了。即便是你把Cell高度手动调整成这样:

屏幕快照 2013-12-23 下午6.11.16

在iOS 6下会显示出正确的结果:

屏幕快照 2013-12-23 下午6.01.50

屏幕快照 2013-12-23 下午5.47.57

源代码下载  
下载页面 
注意:链接是微软SkyDrive页面,下载时请用浏览器直接下载,用某些下载工具可能无法下载  
源代码环境:Xcode 5.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值