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

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

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

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

Q&A

解決済

1回答

1772閲覧

swift5 カスタムセルにあるTextViewの値を取得するdelegateの方法

Cyobi11

総合スコア7

Swift

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

0グッド

1クリップ

投稿2020/01/29 00:34

前提・実現したいこと

初学・独学者です。カスタムセルにあるTextViewに入力した文字列の値をdelegateで取得したいです。

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

プロトコル内の関数をビューコントローラーに用い、その関数内にあるprintが処理されません。
また、TextViewで入力した文字列の値をdelegateで受け取れていないと思っています。

delegateの実装方法の誤っている箇所をご指摘いただき、正しい方法を教えていただけますでしょうか?

全てコードで記入していますので、コピべしていただけると確認できます。

swift5

1import UIKit 2 3protocol TextviewEditCellDelegate: AnyObject { 4 func textviewDidEndEditing(cell: CommentTableViewCell, value: String) 5} 6 7class CommentTableViewCell: UITableViewCell, UITextViewDelegate { 8 let cellCommentLabel: UITextView = { 9 let commentTextView = UITextView() 10 let screenWidth = UIScreen.main.bounds.size.width 11 let screenHeight = UIScreen.main.bounds.size.height 12 commentTextView.frame = CGRect(x: screenWidth * 0.05, y: 0, width: screenWidth * 0.9, height: screenHeight * 0.05) 13 commentTextView.font = .systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 1)) 14 commentTextView.textColor = .white 15 commentTextView.backgroundColor = .black 16 commentTextView.textAlignment = .left 17 commentTextView.isScrollEnabled = false 18 commentTextView.tintColor = .white 19 commentTextView.keyboardAppearance = .dark 20 return commentTextView 21 }() 22 var textChanged: ((String) -> Void)? 23 weak var delegate: TextviewEditCellDelegate? 24 25 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { 26 super.init(style: style, reuseIdentifier: reuseIdentifier) 27 cellCommentLabel.delegate = self 28 setup() 29 } 30 required init?(coder aDecoder: NSCoder) { 31 fatalError("init(coder:) has not been implemented") 32 } 33 private func setup() { 34 contentView.addSubview(cellCommentLabel) 35 addToolBar(textView: cellCommentLabel) 36 } 37 func setUpPartContents(comment: String?) { 38 guard let comment = comment else { return } 39 cellCommentLabel.text = comment 40 } 41 func addToolBar(textView: UITextView) { 42 let toolBar = UIToolbar() 43 toolBar.barStyle = UIBarStyle.default 44 toolBar.isTranslucent = true 45 let doneButton = UIBarButtonItem(title: "実行", style: .done, target: self, action: #selector(CommentTableViewCell.donePressed)) 46 toolBar.setItems([doneButton], animated: false) 47 toolBar.isUserInteractionEnabled = true 48 toolBar.sizeToFit() 49 textView.delegate = self 50 textView.inputAccessoryView = toolBar 51 } 52 @objc func donePressed() { 53 delegate?.textviewDidEndEditing(cell: self, value: cellCommentLabel.text!) 54 cellCommentLabel.resignFirstResponder() 55 } 56 func textChanged(action: @escaping (String) -> Void) { 57 self.textChanged = action 58 } 59 func textViewDidChange(_ textView: UITextView) { 60 textChanged?(textView.text) 61 } 62} 63 64class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, TextviewEditCellDelegate { 65 var sendComment: String? 66 var comment: String? 67 let commentTableViewCell = CommentTableViewCell() 68 fileprivate var tableview: UITableView! 69 fileprivate let screenWidth = UIScreen.main.bounds.size.width 70 fileprivate let screenHeight = UIScreen.main.bounds.size.height 71 let sampleData = ["サンプルデータ"] 72 func textviewDidEndEditing(cell: CommentTableViewCell, value: String) { 73 self.sendComment = value 74 print("デリゲート後のバリューは、(self.sendComment ?? "") です。") 75 let path = tableview.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableview)) 76 } 77 override func viewDidLoad() { 78 super.viewDidLoad() 79 // Do any additional setup after loading the view. 80 setupTableView() 81 commentTableViewCell.delegate = self 82 } 83 func numberOfSections(in tableView: UITableView) -> Int { 84 return sampleData.count 85 } 86 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 87 return sampleData[section].count 88 } 89 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 90 return "サンプル" 91 } 92 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 93 return 40 94 } 95 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 96 let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell",for: indexPath as IndexPath) as! CommentTableViewCell 97 cell.setUpPartContents(comment: sampleData[0]) 98 return cell 99 } 100 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 101 return screenHeight * 0.03 102 } 103 func setupTableView() { 104 tableview = UITableView(frame: CGRect(x: 0, y: screenHeight * 0.35, width: screenWidth, height: screenHeight - (screenHeight * 0.35))) 105 tableview.tableFooterView = UIView(frame: .zero) 106 tableview.register(CommentTableViewCell.self, forCellReuseIdentifier: "commentCell") 107 tableview.rowHeight = UITableView.automaticDimension 108 tableview.estimatedRowHeight = 100000 109 tableview.rowHeight = screenHeight * 0.05 110 tableview.dataSource = self 111 tableview.delegate = self 112 view.addSubview(tableview) 113 } 114}

補足情報(FW/ツールのバージョンなど)

Swift5・Xcode13.2のIOS13で全てコードでの作成です。
皆様のお知恵をお借りできますと幸いです。よろしくお願い致します。

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

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

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

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

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

MasatoUchida

2020/01/29 01:14

donePressed()が呼ばれることと、TextviewEditCellDelegateがnilになっていないか確認お願いします。
Cyobi11

2020/01/29 01:55

お返事ありがとうございます。donePressed()は呼ばれていますが、その呼び出し時にTextviewEditCellDelegate(CommentTableViewCellクラスになるdelegate)は、nilでした。これをnilにしない方法が分からず、解決方法を教えていただけますでしょうか。申し訳ありませんが、よろしくお願い致します。
guest

回答1

0

ベストアンサー

ViewControllerのsuper.viewDidLoadの中でcommentTableViewCell.delegate = selfとしていますが、これはlet commentTableViewCell = CommentTableViewCell()でプロパティに設定しているCommentTableViewCellのdelegateを設定しているだけであり、今画面に表示しているCommentTableViewCellのdelegateを設定しているわけではありません。そのため、今画面に表示しているセルを操作してもdelegateは呼ばれません。

tableView(_:cellForRowAt:)メソッドの中でcell.delegate = selfとすれば、今画面に表示しているセルのdelegateを設定でき、セル内のUITextViewを操作した時にdelegateが呼ばれるようになると思います。

投稿2020/01/29 02:01

TakeOne

総合スコア6299

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

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

Cyobi11

2020/01/29 03:12

お返事ありがとうございます。頂いた方法で実装したところ、上手くいきました!的確な解決方法でしたのでベストアンサーとさせていただきました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問