Swift:如何为UITableView的rowHeight设置动画? [英] Swift: How to animate the rowHeight of a UITableView?

查看:98
本文介绍了Swift:如何为UITableView的rowHeight设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在tableView函数内部调用startAnimation()来动画化tableViewCell行的高度:

I'm trying to animate the height of tableViewCell rows by calling startAnimation() inside the tableView function:

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!")
    })
}

结果:行高确实发生了变化,但没有发生任何动画。我不明白为什么动画不起作用。我应该在某个地方定义一些开始和结束状态吗?

The result: The row height does change but without any animation occurring. I don't understand why the animation doesn't work. Should I perhaps define some beginning and end state somewhere?

推荐答案

请勿以这种方式更改高度。相反,当您知道要更改单元格的高度时,调用(在任何函数中):

Don't change the height that way. Instead, when you know you want to change the height of a cell, call (in whatever function):

self.tableView.beginUpdates()
self.tableView.endUpdates()

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

These calls notify the tableView to check for height changes. Then implement the delegate override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat, and provide the proper height for each cell. The change in height will be animated automatically. You can return UITableViewAutomaticDimension for items you don't have an explicit height for.

我不建议这样做例如,来自 cellForRowAtIndexPath 内的操作,但是响应于 didSelectRowAtIndexPath 的点击。在我的一个课程中,我这样做:

I would not suggest doing such actions from within cellForRowAtIndexPath, however, but in one that responds to a tap didSelectRowAtIndexPath, for example. In one of my classes, I do:

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

这篇关于Swift:如何为UITableView的rowHeight设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆