【Swift,iOS】TableViewでのpresentメソッドの実装方法
解決済
回答 2
投稿
- 評価
- クリップ 1
- VIEW 796
前提・実現したいこと
TableViewに表示したcellにボタンを設置し、ボタンをタップした時にカメラロール(フォトライブラリー)から写真を取得したいです。
(下記画像のプロフィール画像横の"Button")
xibファイルとUITableViewCellで作成しています。
発生している問題・エラーメッセージ
cellではpresentメソッドが使用できない為、どのように解決をすれば良いかわからないでいます。
Use of unresolved identifier 'present'
Value of type 'ProfileTableViewCell' has no member 'dismiss'
該当のソースコード
↓ProfileTableViewCell.swift
import UIKit
class ProfileTableViewCell: UITableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imagePickUpButton: UIButton!
@IBOutlet weak var userImageView: UIImageView!
var picker: UIImagePickerController! = UIImagePickerController()
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func imagePickUpButtonClicked(sender: UIButton) {
picker.sourceType = UIImagePickerController.SourceType.photoLibrary
picker.delegate = self
picker.navigationBar.tintColor = UIColor.white
picker.navigationBar.barTintColor = UIColor.gray
present(picker, animated: true, completion: nil) //エラーになる
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imagePickUpButton.setBackgroundImage(image, for: UIControl.State.normal)
} else {
print("Error")
}
self.dismiss(animated: true, completion: nil) //エラーになる
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// モーダルビューを閉じる
self.dismiss(animated: true, completion: nil) //エラーになる
}
}
↓ProfileViewController.swift
import UIKit
class ProfileViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var profileTableView: UITableView!
var sectionTitlle = [" "," ", " "]
override func viewDidLoad() {
super.viewDidLoad()
profileTableView.delegate = self
profileTableView.dataSource = self
profileTableView.register(UINib(nibName: "ProfileTableViewCell", bundle: nil), forCellReuseIdentifier: "ProfileTableViewCell")
profileTableView.register(UINib(nibName: "BackgroundTableViewCell", bundle: nil), forCellReuseIdentifier: "BackgroundTableViewCell")
profileTableView.register(UINib(nibName: "UserNameTableViewCell", bundle: nil), forCellReuseIdentifier: "UserNameTableViewCell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitlle.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitlle[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell: ProfileTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
return cell
} else if indexPath.section == 1 {
let cell: BackgroundTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BackgroundTableViewCell") as! BackgroundTableViewCell
return cell
} else {
let cell: UserNameTableViewCell = tableView.dequeueReusableCell(withIdentifier: "UserNameTableViewCell") as! UserNameTableViewCell
self.view.endEditing(true)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 15
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// セルの選択解除
profileTableView.deselectRow(at: indexPath, animated: true)
return
}
}
試したこと
下記記事を元に実装しています。
https://qiita.com/takoikatakotako/items/22a29c3992b6e643245a
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
↓こういうextensionを入れて
【Swift】親のView, ViewControllerを取得する
https://qiita.com/tetsukick/items/ae05fdc6040c491639a2
present(picker, animated: true, completion: nil)
↓
parentViewController()?.present(picker, animated: true, completion: nil)
みたいに書けばおそらく動くのではないかと思われます。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
presentやdismissはUIViewControllerの機能なのでUITableViewCellのカスタムクラス内で呼ぼうとしても存在しません。
cell内のボタンで遷移させたい場合は、cell内のボタンにaddActionするメソッドをProfileTableViewCellクラスに用意し、それをProfileViewController暮らすから設定すれば動作するかと思います。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.22%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2019/11/19 21:35
大変助かりました。