更改时为 UITableView 动画设置 reloadData [英] Have a reloadData for a UITableView animate when changing

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

问题描述

我有一个有两种模式的 UITableView.当我们在模式之间切换时,我有不同数量的部分和每个部分的单元格.理想情况下,它会在表格增长或缩小时做一些很酷的动画.

这是我试过的代码,但它没有做任何事情:

CGContextRef context = UIGraphicsGetCurrentContext();[UIView beginAnimations:nil context:context];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];[UIView setAnimationDuration:0.5];[self.tableView reloadData];[UIView commitAnimations];

对我如何做到这一点有任何想法吗?

解决方案

其实很简单:

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

来自文档:><块引用>

调用这个方法会导致表视图询问它的数据源指定部分的新单元格.表格视图动画插入新单元格,同时将旧单元格动画化.

I have a UITableView that has two modes. When we switch between the modes I have a different number of sections and cells per section. Ideally, it would do some cool animation when the table grows or shrinks.

Here is the code I tried, but it doesn't do anything:

CGContextRef context = UIGraphicsGetCurrentContext(); 
[UIView beginAnimations:nil context:context]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:0.5]; 

[self.tableView reloadData];
[UIView commitAnimations];

Any thoughts on how I could do this?

解决方案

Actually, it's very simple:

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

From the documentation:

Calling this method causes the table view to ask its data source for new cells for the specified sections. The table view animates the insertion of new cells in as it animates the old cells out.

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

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