iOS 효율적인 페이징 로드(TableView, CollectionView)

3180 단어

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];

총괄: 비교해 보면 최적화된 후에 코드량이 약간 증가한 것처럼 보이지만 이론적으로 페이지를 나누어 불러오는 성능이 더욱 좋다.이전에 페이지별로 불러오는 전역 리셋은 최적화된 후에 국부 리셋으로 변경되었습니다.따라서 성능이 향상됩니다.

좋은 웹페이지 즐겨찾기