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 functionfunc 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 } }总结
以上是内存溢出为你收集整理的Swift:如何为UITableView的rowHeight设置动画?全部内容,希望文章能够帮你解决Swift:如何为UITableView的rowHeight设置动画?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)