ui

tableView操作刷新块的应用

在介绍动画块之前,我们先看几个函数:

插入分区

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

animation参数是一个枚举,枚举的动画类型如下

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

UITableViewRowAnimationFade,//淡入淡出

UITableViewRowAnimationRight,//从右滑入

UITableViewRowAnimationLeft,//从左滑入

UITableViewRowAnimationTop,//从上滑入

UITableViewRowAnimationBottom,//从下滑入

UITableViewRowAnimationNone,  //没有动画

UITableViewRowAnimationMiddle,

UITableViewRowAnimationAutomatic = 100  // 自动选择合适的动画

};

删除分区

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

重载一个分区

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;

移动一个分区

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;

插入一些行

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

删除一些行

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

重载一些行

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

移动某行

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

了解了上面几个函数,我们来看什么是操作刷新块:

当我们调用的上面的函数时,tableView会立刻调用代理方法进行刷新,如果其中我们所做的操作是删除某行,而然数据源数组我们可能并没有刷新,程序就会崩溃掉,原因是代理返回的信息和我们删除后不符

IOS为我们提供了下面两个函数解决这个问题:

开始块标志

- (void)beginUpdates;

结束快标志

- (void)endUpdates;

我们可以将我们要做的操作全部写在这个块中,那么,只有当程序执行到结束快标志后,才会调用代理刷新方法代码示例如下:

[tab beginUpdates];

[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

[dataArray removeObjectAtIndex:1];

[tab endUpdates];

注意:不要在这个块中调用reloadData这个方法,它会使动画失效

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容