iOS 高效的分页加载实现示例

来源:爱站网时间:2020-05-15编辑:网友分享
本篇文章主要介绍了iOS 高效的分页加载实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

项目开发中,我们经常会接触到分页的效果,而滚动分页和数据接口的分类并不相同,今天爱站技术频道小编就为大家带来了iOS 高效的分页加载实现示例,希望大家都能迅速熟悉这些开发功能,从而提高开发效率。

一、tableview的分页加载的代码对比

没有优化之前的代码如下:

    [strongSelf.tableView.mj_footer endRefreshing];
    [strongSelf.articleArr addObjectsFromArray:feedList];
    [strongSelf.tableView reloadData];

优化之后的代码如下:

    NSMutableArray *indexPaths = [NSMutableArray array];
    [feedList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
      
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(strongSelf.articleArr.count + idx) inSection:0];
      [indexPaths addObject:indexPath];
    }];
    
    [strongSelf.tableView.mj_footer endRefreshing];
    
    [strongSelf.articleArr addObjectsFromArray:feedList];
    
    [strongSelf.tableView beginUpdates];
    [strongSelf.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [strongSelf.tableView endUpdates];

二、collectonview的分页加载的代码对比

没有优化之前的代码如下:

     [strongSelf.feedList addObjectsFromArray:feedList];
    if (feedList.count 

优化之后的代码如下:

    NSMutableArray *indexPaths = [NSMutableArray array];
    [feedList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      
      [indexPaths addObject:[NSIndexPath indexPathForItem:(strongSelf.feedList.count + idx) inSection:0]];
    }];
    
    [strongSelf.feedList addObjectsFromArray:feedList];
    if (feedList.count 

总结:相比较之下,优化之后看似代码量增加了少许,但是从理论上分页加载的性能更好了。之前分页加载使用的全局刷新,优化之后改用了局部刷新。从而性能得到提升。

爱站技术频道小编已经为大家介绍了iOS 高效的分页加载实现示例,而分页开发是帮助每一位刚刚入门的程序员进行相关的指导,能够帮助每一位程序员顺利的进入这个行业。

上一篇:IOS 开发之实现取消tableView返回时cell选中的问题

下一篇:IOS swift中的动画的实例详解

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载