Swift:如何设置UITableView的行高动画?

Swift:如何设置UITableView的行高动画?,swift,uitableview,animation,Swift,Uitableview,Animation,我试图通过在tableView函数中调用startAnimation()来设置tableViewCell行高度的动画: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIn

我试图通过在tableView函数中调用startAnimation()来设置tableViewCell行高度的动画:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell

    tableView.rowHeight = 44.0

    startAnimation(tableView)

    return cell
}

//MARK: Animation function

func startAnimation(tableView: UITableView) {

    UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: {

        tableView.rowHeight = 88.0

    }, completion: { finished in

        print("Row heights changed!")
    })
}
self.tableView.beginUpdates()
self.tableView.endUpdates()

结果:行高度确实会发生更改,但不会发生任何动画。我不明白为什么动画不起作用。我是否应该在某个地方定义一些开始和结束状态

不要那样改变高度。相反,当您知道要更改单元格高度时,可以调用(在任何函数中):

这些调用通知tableView检查高度更改。然后实现委托
覆盖func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat
,并为每个单元格提供适当的高度。高度变化将自动设置动画。对于没有明确高度的项目,可以返回
UITableViewAutomaticDimension

但是,我不建议在
单元格中为rowatinexpath
执行此类操作,而是在一个对点击
didselectrowatinexpath
做出响应的单元格中执行此类操作。在我的一门课上,我做:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath == self.selectedIndexPath {
      self.selectedIndexPath = nil
    }else{
      self.selectedIndexPath = indexPath
    }
  }

internal var selectedIndexPath: NSIndexPath? {
    didSet{
      //(own internal logic removed)

      //these magical lines tell the tableview something's up, and it checks cell heights and animates changes
      self.tableView.beginUpdates()
      self.tableView.endUpdates()
    }
  }

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == self.selectedIndexPath {
      let size = //your custom size
      return size
    }else{
      return UITableViewAutomaticDimension
    }
  }