swift UITableView设置rowHeight
我试图设置tableView的每一行的高度与这个代码对应的单元格的高度: 
 override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { var cell = tableView.cellForRowAtIndexPath(indexPath) return cell.frame.height } 
 初始化var cell时出现这个错误: 
线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x306d2c)
对于设置行高有单独的方法:
对于Swift 3
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100.0;//Choose your custom row height } 
较老的Swift使用
 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 100.0;//Choose your custom row height } 
否则,您可以使用以下设置行高:
 self.tableView.rowHeight = 44.0 
在ViewDidLoad中 。
 把默认的rowHeight放在viewDidLoad或awakeFromNib 。 正如Martin R.指出的,你不能从heightForRowAtIndexPath调用cellForRowAtIndexPath 
 self.tableView.rowHeight = 44.0 
 正如在注释中指出的那样,您不能在heightForRowAtIndexPath调用cellForRowAtIndexPath 。 
你可以做的是创build一个模板单元格,用于填充数据,然后计算其高度。 这个单元格不参与表格渲染,可以重复使用它来计算每个表格单元格的高度。
简而言之,它包括configuration模板单元格与您要显示的数据,使其调整相应的内容,然后读取其高度。
我已经从我正在从事的一个项目中获得这个代码 – 不幸的是它在Objective C中,我不认为你将有问题翻译成swift
 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { static PostCommentCell *sizingCell = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sizingCell = [self.tblComments dequeueReusableCellWithIdentifier:POST_COMMENT_CELL_IDENTIFIER]; }); sizingCell.comment = self.comments[indexPath.row]; [sizingCell setNeedsLayout]; [sizingCell layoutIfNeeded]; CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; return size.height; } 
 yourTableView.rowHeight = UITableViewAutomaticDimension 
尝试这个。
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { var height:CGFloat = CGFloat() if indexPath.row == 1 { height = 150 } else { height = 50 } return height }