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

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

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

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

Swift

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

Q&A

解決済

2回答

403閲覧

tableviewの中にtextviewを配置した場合の値の取得方法を教えて下さい

po_tato

総合スコア97

TableView

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

Swift

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

0グッド

0クリップ

投稿2019/12/16 03:06

apiから情報を取得しメールタイトル、本文を表示し、それに返信可能なメールフォームを作成しているのですが、
tableviewの中にtextviewを配置させる形で進める場合のtextviewの値の受け取り方などについて質問です。

セル1:メールタイトル、日付
セル2:メール本文、
セル3:textView(返信文)、
セル4番目:ImageView(送信ボタン)

問題は送信ボタンを押した際にtextviewに値は入っているのに、下記のコメントアウト部分のように空判定されてしまうことです。

tableviewの中に配置した場合、delegateの問題なのかなと推測しているのですが、
'''
~.delegate = self
'''
のような記述が少ないのでしょうか?
また何か他に問題があるのでしょうか?

イメージ説明

import UIKit import SwiftyJSON import Alamofire class MessageDetailViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CreateDatadelegate,UITextViewDelegate{ @IBOutlet weak var table: UITableView! let sw = DeviceSize.screenWidth() let sh = DeviceSize.screenHeight() var mesage_date_time:String = "" //メッセージ送信日時 var message_title:String = "" //メッセージタイトル var message_main:String = "" //メッセージ本文 var replyMessage:String = "" //ユーザー側返信内容 let version:String = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate override func viewDidLoad() { super.viewDidLoad() table.delegate = self table.dataSource = self self.table.estimatedRowHeight = 200.0 self.table.rowHeight = UITableViewAutomaticDimension } override func viewWillAppear(_ animated: Bool) { let profile = CreateData() profile.delegate = self profile.loadprofile() } //閉じる @objc func commitButtonTapped() { self.view.endEditing(true) } func complete(_ param: [String : String]) {     〜api処理〜 //文字数の都合上割愛しています self.mesage_date_time = jsons["list"]["date"].string! self.message_title = jsons["list"]["title"].string! self.message_main = jsons["list"]["main"].string! self.table.reloadData() }//banner } // Section数 func numberOfSections(in table: UITableView) -> Int { return 4// 表示したいセルの数 } /// セルの個数を指定するデリゲートメソッド(必須) func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } /// セルの値を設定するデリゲートメソッド(必須) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 0{ guard let cell = table.dequeueReusableCell(withIdentifier: "message_detail1", for: indexPath) as? MessageDetailcell1 else { fatalError("Your cellIdentifier is invalid") } cell.layoutWithData(self.message_title, Text2: self.mesage_date_time) return cell }else if indexPath.section == 1{ guard let cell = table.dequeueReusableCell(withIdentifier: "message_detail2", for: indexPath) as? MessageDetailcell2 else { fatalError("Your cellIdentifier is invalid") } cell.layoutWithData(self.message_main) return cell }else if indexPath.section == 2{ guard let cell = table.dequeueReusableCell(withIdentifier: "message_detail3", for: indexPath) as? MessageDetailcell3 else { fatalError("Your cellIdentifier is invalid") } cell.form_txtView.delegate = self        //キーボード閉じる処理// // ツールバー生成 let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) // スタイルを設定 toolBar.barStyle = UIBarStyle.default // 画面幅に合わせてサイズを変更 toolBar.sizeToFit() // 閉じるボタンを右に配置するためのスペース? let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil) // 閉じるボタン let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(MessageDetailViewController.commitButtonTapped)) // スペース、閉じるボタンを右側に配置 toolBar.items = [spacer, commitButton] // textViewのキーボードにツールバーを設定 cell.form_txtView.inputAccessoryView = toolBar return cell }else{ guard let cell = table.dequeueReusableCell(withIdentifier: "message_detail4", for: indexPath) as? MessageDetailcell4 else { fatalError("Your cellIdentifier is invalid") } let imgratio:CGFloat = 142/690 let iw = CGFloat(self.sw) cell.mail_send.image = UIImage(named:"mailsend.png")?.ResizeÜIImage(width: iw, height: iw*imgratio) cell.mail_send.contentMode = UIViewContentMode.scaleAspectFit return cell } } //セルが選択された時 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //self.view.endEditing(true) if indexPath.section == 3{ guard let cell = table.dequeueReusableCell(withIdentifier: "message_detail3", for: indexPath) as? MessageDetailcell3 else { fatalError("Your cellIdentifier is invalid") } self.replyMessage = cell.form_txtView.text print(self.replyMessage) //←textviewに入力した値が反映されず空になる if self.replyMessage == ""{ let alertController = UIAlertController(title: "", message: "メッセージを入力して下さい", preferredStyle: UIAlertControllerStyle.alert) let actionOK:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default ,handler:{ (action:UIAlertAction!) -> Void in print("OK") }) // addActionした順に左から右にボタンが配置されます alertController.addAction(actionOK) self.present(alertController, animated: true, completion: nil) }else{ let alertController = UIAlertController(title: "", message: "メールを送信しますか?", preferredStyle: UIAlertControllerStyle.alert) let actionOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.default ,handler:{ (action:UIAlertAction!) -> Void in //~メール送信処理 割愛~// let alertController = UIAlertController(title: "", message: tm, preferredStyle: UIAlertControllerStyle.alert) let actionOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.default ,handler:{ (action:UIAlertAction!) -> Void in }) alertController.addAction(actionOK) self.present(alertController, animated: true, completion: nil) }) } }) let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:{ (action:UIAlertAction!) -> Void in print("Cancel") }) // addActionした順に左から右にボタンが配置されます alertController.addAction(actionOK) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil) //cell.form_txtView.text = "" //self.table.reloadData() } }else{ return } } } class MessageDetailcell1: UITableViewCell { @IBOutlet weak var msg_title: UILabel! @IBOutlet weak var msg_date: UILabel! func layoutWithData(_ text1:String,Text2 text2:String) { msg_title.text = text1 msg_title.adjustsFontSizeToFitWidth = true msg_date.text = text2 } } class MessageDetailcell2: UITableViewCell { @IBOutlet weak var msg_main: UILabel! func layoutWithData(_ text1:String) { msg_main.text = text1 } } class MessageDetailcell3: UITableViewCell,UITextViewDelegate { @IBOutlet weak var form_txtView: UITextView! func layoutWithData(_ text:String) { form_txtView.text = text } } class MessageDetailcell4: UITableViewCell { @IBOutlet weak var mail_send: UIImageView! }

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

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

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

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

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

guest

回答2

0

tableview(_: didSelectRowAt)メソッドは、これから表示するセルをテーブルビューに返すためのもので、今表示しているものを取得するわけではありません。だから空です。値が入ってくる場合もあるでしょうけど、この値を期待してもうまくいきません。

セルに情報を持たせる今の構造だといろいろとうまく行かないと思うので、構造を根本的に見直す必要があります。vanderlvovさんの言う通り、テーブルビューじゃないほうが楽にできるでしょうね。

投稿2019/12/16 07:34

eytyet

総合スコア803

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

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

0

ベストアンサー

先ず意見です。(もちろん、無視してもいいけど)
正直に言うと、このtableViewの使い方は一般的ではないと思います。

mesage_date_time //メッセージ送信日時 message_title //メッセージタイトル message_main //メッセージ本文 replyMessage //ユーザー側返信内容

を別々のcellに入れるのではなくて、同じのcellのなかでlabelを適当に作れば?
この場合、TableViewControllerではなくて、UIViewControllerを使えば、より簡単に実装できます。
(とりあえず、質問をクリップして、po_tatoさんのアプローチもやってみますけど、tableViewをやめてUIViewControllerで解決した方がいいと思います)

投稿2019/12/16 03:36

vanderlvov

総合スコア685

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

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

po_tato

2019/12/16 08:20

なんでもかんでも、UITableViewを使用してはだめですね。 UIViewControllerを使用して実装してみようと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問