Swift:如何为UITableView的rowHeight设置动画?

我试图通过在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 section:Int) – > CGFloat,为每个细胞提供合适的高度.高度的变化将自动动画.您可以为没有明确高度的项目返回UITableViewAutomaticDimension.

但是,我建议不要在cellForRowAtIndexPath中执行此类操作,而应该在响应一个tapSelectRowAtIndexPath的响应中执行此操作.在我的一个课程中,我做了:

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
    }
  }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...