[iOS] Collection View 무한 스크롤 구현하기

HL·2022년 4월 30일
0

iOS

목록 보기
4/22

요약

셀의 마지막 줄이 나타날 때 쯤 데이터 추가해줌

(1) 특정 셀이 나타나기 직전에 호출됨

// ...
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    self.viewModel.loadIfMoreNeeded(index: indexPath.item)
}
// ...

(2) 끝에서 두번째 줄일 때 데이터 추가

// ...
func loadIfMoreNeeded(index: Int) {
    // 데이터 개수-6 >>> 3*2=6 >>> 두 줄 전에 >>> 미리 로드할거임
    if index > data.count - 6 {
        self.load()	// 데이터 추가하는 함수
    }
}
// ...
profile
Frontend 개발자입니다.

0개의 댓글