iOS 自定格单元

示例

苹果在iOS 8中引入了自动调整大小单元。使用Autolayout显式地布局UITableViewCells,UITableView会为您处理其余的工作。行高是自动计算的,默认rowHeight值为UITableViewAutomaticDimension。

estimatedRowHeight自调整单元格计算时使用UITableView属性。

创建自调整大小的表格视图单元格时,需要设置此属性并使用约束来定义单元格的大小。

-苹果的UITableView文档

self.tableView.estimatedRowHeight = 44.0

请注意,如果要为所有单元格设置动态高度,heightForRowAtIndexPath则不需要tableView的委托。在必要时以及重新加载或加载表格视图之前,只需设置以上属性即可。但是,您可以通过以下功能设置特定单元格的高度,同时使其他单元格动态化:

迅速

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switchindexPath.section{
    case 1:
        return 60
    default:
        return UITableViewAutomaticDimension
    }
}

目标C

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  switch (indexPath.section) {
      case 1:
        return 60;
      default:
        return UITableViewAutomaticDimension;
    }  
}