개발/iOS
UICollectionView의 cellForItem이 nil이 나올 때
빙수킹
2020. 11. 25. 19:17
문제 상황
UICollectionView의 특정 셀을 디폴트로 설정해놓고 어떤 효과를 주고 싶었음.. 그러나 cellForItem을 사용해서
collectionView.cellForItem(at: IndexPath(row: strongSelf.currentPage.rawValue, section: 0))
위 코드를 실행했는데 nil이 나왔음.
컬렉션 뷰가 있고 셀을 다 넣어줬는데 왜??????
이유
Apple Developer Documentation
developer.apple.com
여기 보면 "visible cell"을 가져온다고 되어있었다..
해결방안
먼저 offset을 세팅해서 해당 셀이 보이는 상태로 스크롤을 맞춰놓고 cell 호출했다.
- 주의: 여기서 setContentOffset animated: "true" 하면 또 안됨. animate 하느라 바로 세팅이 안되나봄??
let offset = CGPoint.init(x: CGFloat(self.currentPage.rawValue) * self.contentsViewWidth, y: 0)
self.collectionView.setContentOffset(offset, animated: false)