質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

2488閲覧

【Swift,iOS】TableViewでのpresentメソッドの実装方法

duck015

総合スコア29

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

2クリップ

投稿2019/11/19 07:35

前提・実現したいこと

TableViewに表示したcellにボタンを設置し、ボタンをタップした時にカメラロール(フォトライブラリー)から写真を取得したいです。
(下記画像のプロフィール画像横の"Button")
xibファイルとUITableViewCellで作成しています。
イメージ説明

発生している問題・エラーメッセージ

cellではpresentメソッドが使用できない為、どのように解決をすれば良いかわからないでいます。

Use of unresolved identifier 'present' Value of type 'ProfileTableViewCell' has no member 'dismiss'

該当のソースコード

↓ProfileTableViewCell.swift

Swift

1import UIKit 2 3class ProfileTableViewCell: UITableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 4 5 6 @IBOutlet weak var imagePickUpButton: UIButton! 7 @IBOutlet weak var userImageView: UIImageView! 8 var picker: UIImagePickerController! = UIImagePickerController() 9 10 override func awakeFromNib() { 11 super.awakeFromNib() 12 13 } 14 15 override func setSelected(_ selected: Bool, animated: Bool) { 16 super.setSelected(selected, animated: animated) 17 } 18 19 func imagePickUpButtonClicked(sender: UIButton) { 20 picker.sourceType = UIImagePickerController.SourceType.photoLibrary 21 picker.delegate = self 22 picker.navigationBar.tintColor = UIColor.white 23 picker.navigationBar.barTintColor = UIColor.gray 24 present(picker, animated: true, completion: nil) //エラーになる 25 } 26 27 28 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 29 if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { 30 imagePickUpButton.setBackgroundImage(image, for: UIControl.State.normal) 31 } else { 32 print("Error") 33 } 34 self.dismiss(animated: true, completion: nil) //エラーになる 35 } 36 37 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 38 // モーダルビューを閉じる 39 self.dismiss(animated: true, completion: nil) //エラーになる 40 } 41} 42

↓ProfileViewController.swift

Swift

1import UIKit 2 3class ProfileViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 4 5 @IBOutlet weak var profileTableView: UITableView! 6 7 var sectionTitlle = [" "," ", " "] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 profileTableView.delegate = self 12 profileTableView.dataSource = self 13 profileTableView.register(UINib(nibName: "ProfileTableViewCell", bundle: nil), forCellReuseIdentifier: "ProfileTableViewCell") 14 profileTableView.register(UINib(nibName: "BackgroundTableViewCell", bundle: nil), forCellReuseIdentifier: "BackgroundTableViewCell") 15 profileTableView.register(UINib(nibName: "UserNameTableViewCell", bundle: nil), forCellReuseIdentifier: "UserNameTableViewCell") 16 } 17 18 func numberOfSections(in tableView: UITableView) -> Int { 19 return sectionTitlle.count 20 } 21 22 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 23 return sectionTitlle[section] 24 } 25 26 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 return 1 28 } 29 30 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 31 if indexPath.section == 0 { 32 let cell: ProfileTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell 33 return cell 34 } else if indexPath.section == 1 { 35 let cell: BackgroundTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BackgroundTableViewCell") as! BackgroundTableViewCell 36 return cell 37 } else { 38 let cell: UserNameTableViewCell = tableView.dequeueReusableCell(withIdentifier: "UserNameTableViewCell") as! UserNameTableViewCell 39 self.view.endEditing(true) 40 return cell 41 } 42 } 43 44 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 45 return 50 46 } 47 48 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 49 return 15 50 } 51 52 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 53 // セルの選択解除 54 profileTableView.deselectRow(at: indexPath, animated: true) 55 return 56 } 57}

試したこと

下記記事を元に実装しています。
https://qiita.com/takoikatakotako/items/22a29c3992b6e643245a

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

↓こういうextensionを入れて

【Swift】親のView, ViewControllerを取得する
https://qiita.com/tetsukick/items/ae05fdc6040c491639a2

present(picker, animated: true, completion: nil)

parentViewController()?.present(picker, animated: true, completion: nil)

みたいに書けばおそらく動くのではないかと思われます。

投稿2019/11/19 07:54

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

duck015

2019/11/19 12:35

回答ありがとございます! 大変助かりました。
guest

0

presentやdismissはUIViewControllerの機能なのでUITableViewCellのカスタムクラス内で呼ぼうとしても存在しません。
cell内のボタンで遷移させたい場合は、cell内のボタンにaddActionするメソッドをProfileTableViewCellクラスに用意し、それをProfileViewController暮らすから設定すれば動作するかと思います。

投稿2019/11/19 07:46

編集2019/11/19 07:47
MasatoUchida

総合スコア134

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

duck015

2019/11/19 12:34

回答ありがとうございます。 無事解決できました! 大変助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問