iOS 효율 적 인 페이지 로 딩 구현 예제

오늘 review 코드 를 발 견 했 을 때 이전의 tableview 와 collectview 의 페이지 로 딩 논리 가 최적화 될 여지 가 있 음 을 발견 하여 최적화 되 었 습 니 다.
1.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];
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];
결론:비교 해 보면 최적화 한 후에 코드 량 이 약간 증가 한 것 처럼 보이 지만 이론 적 으로 페이지 를 나 누 어 로드 하 는 성능 이 더욱 좋다.이전 페이지 로 딩 에 사 용 된 전역 새로 고침 을 최적화 한 후 부분 새로 고침 으로 바 꾸 었 습 니 다.성능 이 향상 된다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기