본문 바로가기

Project/HiddenGem

🤷 컬렉션뷰에 페이징 기능 추가 (스크롤하면 새 데이터 불러와 컬렉션뷰로 보여주기)

✅ 최종 구현 영상

컬렉션뷰를 스크롤해서 마지막 셀에 도달하면 그 다음 데이터를 불러와 보여주도록 함

 

 

✅ 목표

  • 컬렉션뷰에서 마지막 셀 근처까지 스크롤했을 때만 다음 페이지를 요청하고 싶다.
  • 예: 마지막 셀에서 1~2개 남았을 때 미리 로딩.
extension CategoryViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let itemCount = categoriesViewModel.eateryFromCategory.count

        // 마지막 셀에 도달했을 때
        if indexPath.item == itemCount - 1 && !categoriesViewModel.isLoading {
            pageNo += 1
            fetchEateryFromCategory()
        }
    }
}