iOS 응용 (swift)
[iOS - swift] UITableView, UICollectionView, 스크롤 이동 시키는 방법
jake-kim
2021. 10. 21. 00:04

UITableView에서 스크롤 이동

- tableView.scrollToRow(at:at:animated:) 메서드 사용
- UI적인 접근이 필요하므로 main 스레드에서 접근
@IBAction func didTapButton(_ sender: Any) {
DispatchQueue.main.async { [weak self] in
self?.tableView.scrollToRow(at: IndexPath(row: 77, section: 0), at: .bottom, animated: true)
}
}
CollectionView에서 스크롤 이동

- collectionView.scrollToItem(at:at:animated:) 메서드 이용
- main 스레드로 접근
@objc func didTapMoveScrollButton() {
DispatchQueue.main.async { [weak self] in
self?.collectionView.scrollToItem(at: IndexPath(row: 77, section: 0), at: .bottom, animated: true)
}
}
* 전체 소스 코드: https://github.com/JK0369/ExTableView
활용
- 이미지 리스트를 tableView나 collectionView로 조회 > 셀을 탭하여 상세화면에서 가로로 이미지를 스크롤하고난 후 리스트 화면으로 넘어오는 경우, 가로로 이미지를 스크롤한 위치로 자동으로 scroll되어 있는 기능 구현에 사용

* 참고
- scrollToRow(at:at:animated:): https://developer.apple.com/documentation/uikit/uitableview/1614997-scrolltorow
- scrollToItem(at:at:animated:): https://developer.apple.com/documentation/uikit/uicollectionview/1618046-scrolltoitem