###追記
関係ない所かもしれないのですが、画面外に表示されているセルに文字列を入力して
画面を上下に動かすと入力したセルがずれて動くという状態になっていることがわかりました。
###前提・実現したいこと
度々の質問大変申し訳ございません。
現在テーブルビューのカスタムセルにテキストフィールドを一つだけ設置し、
そのテキストフィールドに入力する際にキーボードが重なった場合
そのビューを上にずらして見えるようにするものを実現したいです
###発生している問題・エラーメッセージ
サイトを参考にし、以下のようにスクリプトしてみたのですが
アニメーション自体はするのですが、問題として
・選択した時点でアニメーションしない
・入力を始めるとゆっくりと画面の一番上にスクロールする
ということになり、入力しようとしているテキストフィールドの場所までアニメーションできませんでした
こちらのサイトを参考にしたのですが、やはりテーブルビュー並びにカスタムセルを使って動かす場合はまた違う設定をするのでしょうか・・・。
今回の悪いところ、または参考になるURLをご教示いただけると大変嬉しく思います。
宜しくお願い致します。
``
@IBOutlet var firstTextField: UITextField!
サイトの中にありましたこの部分(画像のテキストフィールド)を接続しますと
Main.storyboard: error: Illegal Configuration:
The a outlet from the RandomController
to the Text_Custom is invalid.
Outlets cannot be connected to repeating content.
とでて繰り返し利用するものに直接接続はしてはならないというエラーが出てしまい、検索やライブラリなど探して見たのですがうまく起動させる方法を見つけられませんでした・・・。
swift
1//viewcontroller 2import UIKit 3 4class RandomController: UIViewController, UITableViewDelegate,UITableViewDataSource,InputTextTableCellDelegate,UITextFieldDelegate { 5 6 var delegate: InputTextTableCellDelegate! = nil 7 8 @IBOutlet weak var tableView: UITableView! 9 //@IBOutlet var textField: UITextField! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 //デリゲートとデータソースを設定 15 tableView.delegate = self 16 tableView.dataSource = self 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 } 22 23 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 return 10; 26 } 27 28 func numberOfSections(in tableView: UITableView) -> Int { // sectionの数を決める 29 return 1 30 } 31 32 33 func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath)->UITableViewCell{ 34 let cell: InputTextTableCell = tableView.dequeueReusableCell(withIdentifier: "InputTextCell",for: indexPath) as! InputTextTableCell 35 cell.delegate = self 36 return cell 37 } 38 39 // MARK: - UITableViewDelegate 40 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 41 return InputTextTableCell.height() 42 } 43 44 //セルの番号、並びにテキストフィールドの文字列を取得する 45 @nonobjc internal func textFieldDidEndEditing(cell: InputTextTableCell, value: NSString) -> () { 46 let path = tableView.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableView)) 47 NSLog("row = %d, value = %@", path!.row, value) 48 } 49 50 //テキストフィールドとキーボードが重ならないように移動 51 override func viewWillAppear(_ animated: Bool) { 52 53 super.viewWillAppear(animated) 54 self.configureObserver() 55 56 } 57 58 //入力が終わったら下げる 59 override func viewWillDisappear(_ animated: Bool) { 60 61 super.viewWillDisappear(animated) 62 self.removeObserver() // Notificationを画面が消えるときに削除 63 } 64 65 66 //Notificationを設定 67 func configureObserver() 68 { 69 let notification = NotificationCenter.default 70 notification.addObserver(self,selector:#selector(keyboardWillShow(notification:)),name: NSNotification.Name.UIKeyboardWillShow,object: nil) 71 notification.addObserver(self,selector:#selector(keyboardWillHide(notification:)),name: NSNotification.Name.UIKeyboardWillShow,object: nil) 72 } 73 74 //Notificationを削除 75 func removeObserver() 76 { 77 let notification = NotificationCenter.default 78 notification.removeObserver(self) 79 } 80 81 //キーボードが出た時、画面全体をずらしていく 82 func keyboardWillShow(notification: Notification?) 83 { 84 let rect = (notification?.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 85 let duration: TimeInterval? = notification?.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double 86 UIView.animate(withDuration: duration!,animations: { () in 87 let transform = CGAffineTransform(translationX: 0, y: -(rect?.size.height)!) 88 self.view.transform = transform 89 }) 90 } 91 92 //キーボードが消えた時に、画面を戻す 93 func keyboardWillHide(notification: Notification?) 94 { 95 let duration: TimeInterval? = notification?.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? Double 96 UIView.animate(withDuration: duration!,animations:{() in 97 self.view.transform = CGAffineTransform.identity 98 }) 99 } 100 101}
swift
1//custom cell 2import UIKit 3 4protocol InputTextTableCellDelegate { 5 func textFieldDidEndEditing(cell: InputTextTableCell, value: NSString) -> () 6} 7 8class InputTextTableCell: UITableViewCell,UITextFieldDelegate { 9 10 var delegate: InputTextTableCellDelegate! = nil 11 12 @IBOutlet weak var textField: UITextField! 13 14 override func awakeFromNib() { 15 super.awakeFromNib() 16 17 textField.returnKeyType = UIReturnKeyType.done 18 textField.delegate = self 19 } 20 21 override func setSelected(_ selected: Bool, animated: Bool) { 22 super.setSelected(selected, animated: animated) 23 } 24 25 static func height() -> CGFloat { 26 return 75.0 27 } 28 29 internal func textFieldShouldReturn(_ textField: UITextField) -> Bool { 30 textField.resignFirstResponder() 31 return true 32 } 33 34 internal func textFieldDidEndEditing(_ textField: UITextField) { 35 self.delegate.textFieldDidEndEditing(cell: self, value: textField.text! as NSString) 36 } 37}
###補足事項
Swift3、Xcode8.3で制作しております。
回答2件
あなたの回答
tips
プレビュー