티스토리 뷰
728x90
반응형
Cell 꾹 눌러서 미리보기 만들기 - context menu 만들기
사용방법
UITableViewDelegate 의 메서드를 이용하면 된다.
func tableView(_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration? {
//code
}
문제
- previewProvider 파라미터에 그냥 뷰컨을 넣었더니 다음과 같은 오류가 발생했다.

그래서 개발자 문서에서 previewProvider를 찾아보았다.

void 로 들어가서 UIViewController 로 리턴하는 클로저를 넘겨주어야했다.
해결

방법1
- previewProvider 에 맞는 형태의 함수를 만들어서 뷰컨의 프로퍼티를 지정해준 후 리턴해주었다.
func makeContextMenu() -> UIViewController {
let storyboard = UIStoryboard(name: "MyTab", bundle: nil)
guard let vc = storyboard.instantiateViewController(identifier: "MyTabVC") as? MyTabVC else {
return UIViewController()
}
vc.name = name
vc.profileImage = imageName
return vc
}
- tableView(_ tableView:contextMenuConfigurationForRowAt indexPath:point:) 메서드에서 UIMenu 를 만들어주었다.
func tableView(_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration? {
let chat = UIAction(title: "채팅하기") { _ in }
let voiceTalk = UIAction(title: "보이스톡") { _ in }
let faceTalk = UIAction(title: "페이스톡") { _ in }
let gift = UIAction(title: "선물하기") { _ in }
//해당 클래스에 선언해준 변수.
//makeContextMenu 메서드에서 뷰컨의 프로퍼티를 지정해주기위해서 사용할 것이다.
self.imageName = friendList[indexPath.row].image
self.name = friendList[indexPath.row].name
return UIContextMenuConfiguration(identifier: nil, previewProvider: makeContextMenu) { _ in
UIMenu(title: "", children: [chat, voiceTalk, faceTalk, gift])
}
}
방법2
- 위와 같은 코드이다. 하지만 뷰컨을 인스턴스화하는 코드가 tableView(_ tableView:contextMenuConfigurationForRowAt indexPath:point:) 메서드 안에 있다.
- 달라진 점이라면 뷰컨을 인스턴스화할 때 else 구문에서
UIContextMenuConfiguration()
를 리턴하는 점과 previewProvider 파라미터에{ return vc }
클로저가 들어가는 점이다.
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let chat = UIAction(title: "채팅하기") { _ in }
let voiceTalk = UIAction(title: "보이스톡") { _ in }
let faceTalk = UIAction(title: "페이스톡") { _ in }
let gift = UIAction(title: "선물하기") { _ in }
let storyboard = UIStoryboard(name: "MyTab", bundle: nil)
guard let vc = storyboard.instantiateViewController(identifier: "MyTabVC") as? MyTabVC else {
return UIContextMenuConfiguration()
}
vc.name = friendList[indexPath.row].name
vc.profileImage = friendList[indexPath.row].image
return UIContextMenuConfiguration(identifier: nil, previewProvider: { return vc }) { _ in
UIMenu(title: "", children: [chat, voiceTalk, faceTalk, gift])
}
}
출처
apple developer - tableView(_:contextMenuConfigurationForRowAt:point:)
728x90
반응형
'iOS' 카테고리의 다른 글
iOS) UIButton programmatically (0) | 2021.07.21 |
---|---|
iOS) convert UIColor to String (0) | 2021.07.21 |
iOS) UINavigationBar 의 large title 색 설정 (0) | 2021.07.21 |
iOS) push 와 pop 시 ViewLifeCycle (0) | 2021.07.21 |
iOS) UITextField 에 입력시 키보드 위 toolbar 구현 (0) | 2021.07.21 |
TAG
- WWDC22
- IOS
- Notification
- Objective-C
- SwiftUI
- MOYA
- UserDefaults
- YPImagePicker
- WidgetKit
- MVVM
- github
- Protocol
- APNS
- RxCocoa
- 2022 KAKAO TECH INTERNSHIP
- Swift
- rxswift
- 서버통신
- containerBackground
- async/await
- WWDC
- Firebase
- configurable widget
- CloneCoding
- Algorithm
- Widget
- watchOS
- urlsession
- projectsetting
- OpenSourceLibrary
최근에 올라온 글
최근에 달린 댓글
글 보관함
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
링크
- Total
- Today
- Yesterday