加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:26:44 所属栏目:百科 来源:网络整理
导读:我试图通过在tableView函数中调用startAnimation()来为tableViewCell行的高度设置动画: func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) - UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifi
我试图通过在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
    }
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读