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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

Q&A

解決済

1回答

1133閲覧

【急募】【Swift】セルのtextviewが可変にならず1行になる

nakamu

総合スコア82

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

0グッド

0クリップ

投稿2019/04/11 10:49

編集2019/04/11 10:51

セルが可変になってないのかtextviewが可変になってないのかわかりませんが、セル内のtextviewが1行になってしまいデータの一部しかテキストが表示されません

Swift

1 2import UIKit 3import Firebase 4import FirebaseDatabase 5 6class ChatViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UITextFieldDelegate, UITextViewDelegate { 7 8 9 @IBOutlet weak var scrollView: UIScrollView! 10 @IBOutlet weak var tableView: UITableView! 11 @IBOutlet weak var inputWrap: UIView! 12 @IBOutlet weak var textField: UITextField! 13 14 var ref: DatabaseReference! 15 var communityId: String! 16 var messageArr:[[String:Any]] = [] 17 var padding: CGPoint = CGPoint(x: 6.0, y: 0.0) 18 var test:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0) 19 var testCounter = 0 20 21 override func viewWillAppear(_ animated: Bool) { 22 23 super.viewWillAppear(animated) 24 25 let notificationCenter = NotificationCenter.default 26 notificationCenter.addObserver(self, selector: #selector(ChatViewController.handleKeyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil) 27 notificationCenter.addObserver(self, selector: #selector(ChatViewController.handleKeyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) 28 29 } 30 31 override func viewDidLoad() { 32 super.viewDidLoad() 33 tableView.delegate = self 34 tableView.dataSource = self 35 self.textField.delegate = self 36 37 textField.returnKeyType = .done 38 39 // 背景色設定 40 self.view.backgroundColor = UIColor.clear 41 tableView.backgroundColor = UIColor.clear 42 inputWrap.backgroundColor = UIColor.clear 43 textField.backgroundColor = UIColor.clear 44 45 self.tableView.rowHeight = UITableView.automaticDimension 46 self.tableView.estimatedRowHeight = 100000 47 48 // ボーダー設定 49 let border = CALayer() 50 border.frame = CGRect(x: 0, y: 0, width: textField.frame.size.width, height: textField.frame.size.height) 51 border.borderColor = UIColor.white.cgColor 52 border.borderWidth = CGFloat(1.5) 53 border.cornerRadius = 20 54 textField.layer.addSublayer(border) 55 textField.attributedPlaceholder = NSAttributedString(string: "メッセージを入力", attributes: [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 255/255, green: 255/255, blue: 255/255, alpha: 0.5)]) 56 57 // 初期化 58 ref = Database.database().reference() 59 60 // 自作セルをテーブルビューに登録する 61 let chatXib = UINib(nibName: "ChatTableViewCell", bundle: nil) 62 tableView.register(chatXib, forCellReuseIdentifier: "chatCell") 63 64 // メッセージ取得 65 ref.child("messages").child(communityId).observe(.childAdded, with: { (snapshot) -> Void in 66 67 let val = snapshot.value as! [String:Any] 68 69 self.messageArr.append(val) 70 print(self.messageArr) 71 72 // データの追加 73 self.tableView.reloadData() 74 75 }) 76 } 77 78 func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 79 // セル生成 80 let cell = table.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! ChatTableViewCell 81 82 let imgView = cell.userImg as! UIImageView 83 let name = cell.userName as! UILabel 84 let text = cell.userMessage as! UITextView 85 86 text.isEditable = false 87 text.delegate = self 88 // 画像設定 89 let img = UIImage(named: "User") 90 imgView.image = img 91 // 配色 92 cell.backgroundColor = UIColor.clear 93 name.backgroundColor = UIColor.clear 94 text.backgroundColor = UIColor.clear 95 // paddingを消す 96 text.textContainerInset = UIEdgeInsets.zero 97 text.textContainer.lineFragmentPadding = 0 98 text.text = (messageArr[indexPath.row]["message"] as! String) 99 100 let height = text.sizeThatFits(CGSize(width: text.frame.size.width, height: CGFloat.greatestFiniteMagnitude)).height 101 text.heightAnchor.constraint(equalToConstant: height).isActive = true 102 103 return cell 104 } 105 106 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 107 return messageArr.count 108 } 109 110 @IBAction func tapScreen(_ sender: Any) { 111 // キーボードを閉じる 112 self.view.endEditing(true) 113 } 114 115 @objc func handleKeyboardWillShowNotification(_ notification: Notification) { 116 117 118 let userInfo = notification.userInfo! 119 let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 120 let myBoundSize: CGSize = UIScreen.main.bounds.size 121 // テキストフィールドの下辺 122 let txtLimit = inputWrap.frame.origin.y + inputWrap.frame.height + 8.0 123 // キーボードの上辺 124 let kbdLimit = myBoundSize.height - keyboardScreenEndFrame.size.height 125 126 127 print("テキストフィールドの下辺:((txtLimit))") 128 print("キーボードの上辺:((kbdLimit))") 129 130 // キーボードの位置の方が上の場合 131 if txtLimit >= kbdLimit { 132 // 上にスライド 133 scrollView.contentOffset.y = txtLimit - kbdLimit 134 } 135 } 136 137 @objc func handleKeyboardWillHideNotification(_ notification: Notification) { 138 scrollView.contentOffset.y = 0 139 } 140 141 //Enterを押したらキーボードが閉じるようにするためのコードです。 142 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 143 // TextFieldから文字を取得 144 let message = textField.text 145 if message == "" { 146 return false 147 } 148 let key = ref.child("messages").childByAutoId().key 149 self.ref.child("messages").child(communityId).child(key!).setValue(["user":RootTabBarController.userId,"message":message]) 150 // TextFieldの中身をクリア 151 textField.text = "" 152 testCounter = 1 153 154 textField.resignFirstResponder() 155 return true 156 } 157} 158

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

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

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

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

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

guest

回答1

0

自己解決

これが原因だった

Swift

1let height = text.sizeThatFits(CGSize(width: text.frame.size.width, height: CGFloat.greatestFiniteMagnitude)).height 2 text.heightAnchor.constraint(equalToConstant: height).isActive = true

投稿2019/04/11 13:50

nakamu

総合スコア82

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問