iOS 효율적인 페이징 로드(TableView, CollectionView)
1. 테이블 뷰의 페이지에 불러오는 코드 대비
최적화되기 전의 코드는 다음과 같다
[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];
2. 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];
총괄: 비교해 보면 최적화된 후에 코드량이 약간 증가한 것처럼 보이지만 이론적으로 페이지를 나누어 불러오는 성능이 더욱 좋다.이전에 페이지별로 불러오는 전역 리셋은 최적화된 후에 국부 리셋으로 변경되었습니다.따라서 성능이 향상됩니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.