ios8 Autolayout实现uitableviewcell 高度自适应

来源:互联网 发布:怎么更换路由器mac地址 编辑:程序博客网 时间:2024/06/05 00:39

github 学习网址    https://github.com/xhzengAIB/iOS8SelfSizingCells


在iOS8以下,如果需要实现一个不同高度的Cell,那需要你手动动态计算高度,这便是一个繁琐的事情,而且富有挑战性,需要把空间复杂度转换为时间复杂度之类的优化。而在iOS8新的SDK里面提供了self sizing cells新功能,已经不再需要手动计算高度啦!不过还需要满足以下三个条件:

1、使用Autolayout进行UI布局约束。 2、指定你的TableView的estimatedRowHeight属性的默认值。 3、指定你的TableView的rowHeight属性为UITableViewAutomaticDimension。

总体而言,伪代码如下:

- (void)viewDidload {    self.yourTableView.estimatedRowHeight = 44.0;    self.yourTableView.rowHeight = UITableViewAutomaticDimension;}

详细的做法可以参考一下Demo。

去掉计算高度的TableViewDelegate方法。

永远不用实现- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法和- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath方法。

0 0