일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- IOS
- Decodable
- uikit
- avcapturesession
- Pod
- iOS 화면 가리기
- ios외부디바이스연결
- AVFoundation
- programmers
- usb카메라연결
- cocoapods
- 정보처리기사 실기
- Xcode
- 2018 KAKAO BLIND RECRUITMENT
- 프로그래머스
- JSONParser
- 정보처리기사 실기 요약본
- CustomCode
- Codable
- swift
- 정보처리기사
- UITableView
- iOS 화면 보호기
- ios카메라유선연결
- JSON
- 위클리챌린지
- parse
- ios외부카메라연결
- 앱 스위처
- 외부카메라감지
- Today
- Total
iOS 개발일기
[Swift] UICollectionView 'didSelectItemAt' 함수가 호출되지 않는 이유 본문
func tableView(
_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath
) { ... }
func collectionView(
_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath
) { ... }
위 두 함수가 호출되지 않는 이유
1. delegate를 설정해주지 않았을 경우
tableView.delegate = self
collectionView.delegate = self
위 같은 방법은 단순한 방법이라 실수를 했을 경우가 많을 것 같네요.
2. tableView 또는 collectionView의 상위 뷰가 'tapGesture'를 가지고 있는 경우'
이 경우는 좀 특별한 상황인 것 같네요.
'호출이 안된다'가 아니라 두 손가락으로 탭하거나 길게 누르거나 혹은 PanGesture가 호출될때 'didSelectItemAt' 함수가 호출이 되더라구요.
이게 왜 이러나 싶어서 엄청 찾아봤습니다.
UICollectionView's didSelectItemAtIndexPath only called when selecting cell with two fingers
I'm using an UICollectionView added on top of a view with a single tap gesture recogniser. The CollectionView makes use of custom cells without any subviews. The delegate's method - (void)
stackoverflow.com
이 글에서 답을 찾아버렸지 뭡니까
만약 UITableView 또는 UICollectionView 가 하위뷰로 포함이 되어있고,
상위 뷰에 UITapGestureRecognizer 가 추가된 상태일 경우에는 'didSelectItemAt' 의 함수가 한 손가락으로 클릭했을 경우 호출되지 않는다라고 하더라고요.
class ViewController: UIViewController {
lazy var tableView: UITableView = { ... }
override
func viewDidLoad() {
super.viewDidLoad()
//tableView를 포함한 상위 뷰에서 'tapGesture'가 추가된 경우
view.addGestureRecognizer(
UITapGestureRecognizer(
target: self,
action: #selector(dismissKeyboard)
)
)
//tableView 화면구성
view.addSubview(tableView)
...
}
@objc
private func dismissKeyboard() {
...
}
}
위와 같은 경우 처럼요.
이런 경우 'cancelsTouchesInView' 속성을 false로 설정해주시면 tableView의 'didSelectItemAt' 함수를 한 손가락 터치로 호출 할 수 있게 됩니다.
cancelsTouchesInView | Apple Developer Documentation
A Boolean value that determines whether touches are delivered to a view when a gesture is recognized.
developer.apple.com
let tap = UITapGestureRecognizer(
target: self,
action: #selector(dismissKeyboard)
)
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
아직 이 외 다른 함수들에 대해서도 좀 더 알아보고 공부해야되겠네요.
'iOS > Swift' 카테고리의 다른 글
[Swift] UIKit 환경에서 Property Wrapper 사용하기 (0) | 2025.03.17 |
---|---|
[Swift] UITableView Dynamic Height (0) | 2024.12.26 |
[Swift] JSONParser - JSONSerialization (0) | 2022.03.16 |
[Swift] JSONParser - Decodable(2) (0) | 2022.03.15 |
[Swift] JSONParser - Decodable(1) (0) | 2022.03.14 |