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! }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。