当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 高效的分页加载实现示例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

今天在review代码的时候发现之前的tableview 和 collectview 的分页加载逻辑还有优化的余地,于是进行了优化。

一、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 < kpagesize) {
      
      [strongself.collectionview.mj_footer endrefreshingwithnomoredata];
    }else{
      
      [strongself.collectionview.mj_footer resetnomoredata];
    }
    [strongself.collectionview reloaddata];

优化之后的代码如下:

    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 < kpagesize) {
      
      [strongself.collectionview.mj_footer endrefreshingwithnomoredata];
    }else{
      
      [strongself.collectionview.mj_footer resetnomoredata];
    }
    [strongself.collectionview insertitemsatindexpaths:indexpaths];

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网