更改单个单元格UITableView的高度

我想要像下面这样的东西:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell if indexPath == 3{ cell.height = "50dp" } return cell } 

这是什么select或最简单的方法呢?

编辑

是否可以指定章节号码:ie-

 if sectionIndex == 5 

我build议使用heightForRowAtIndexPath函数。 TableView将调用此函数来确定特定索引path的行高。

示例代码:

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.section == SECTION_INDEX { return 60 } else { return UITableViewAutomaticDimension } } 

Swift 4:

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.section == SECTION_INDEX { return 60 } else { return UITableViewAutomaticDimension } } 

有一点解释:indexPath有两个属性:section和row。 通过检查这两个属性,可以使您自己的控制stream程来确定每个单元格的高度。

如果你返回60,这意味着你希望细胞的高度是60分。 如果您返回UITableViewAutomaticDimension,您希望系统为您决定最佳高度(在这种情况下,最好为storyboard中的单元格设置自动布局)。

我也build议你在iTunes U上使用斯坦福大学CS193p课程,这对你有很大的帮助:)

最简单的方法是覆盖heightForRowAtIndexPath

  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let section = indexPath.section let row = indexPath.row if section == 0 && row == 2{ return 50.0 } return 22.0 } 
 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{ if indexPath.section == 5 { if indexPath.row == 3 { return 150.0 } } return 200.0 }