国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS 高效的分頁加載實現示例

2020-07-26 02:39:30
字體:
來源:轉載
供稿:網友

今天在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];

總結:相比較之下,優化之后看似代碼量增加了少許,但是從理論上分頁加載的性能更好了。之前分頁加載使用的全局刷新,優化之后改用了局部刷新。從而性能得到提升。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南汇区| 茂名市| 从江县| 丹凤县| 太湖县| 静海县| 通海县| 贵溪市| 灌南县| 炉霍县| 山西省| 赫章县| 会昌县| 比如县| 合川市| 台东县| 青岛市| 辽阳市| 盘锦市| 合江县| 昌黎县| 日喀则市| 南城县| 迭部县| 宁阳县| 九龙坡区| 栖霞市| 凤山县| 微博| 泰兴市| 长寿区| 社旗县| 晋宁县| 星座| 安顺市| 额尔古纳市| 广昌县| 靖州| 巴林右旗| 平凉市| 营口市|