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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

1回答

3351閲覧

【Swift】TableViewのセルに、TextFieldを入れているんですがキーボードを閉じる処理がうまくいきません。

NobumitsuHata

総合スコア141

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2016/10/29 07:26

textfieldのデリゲートを今はコメントアウトしていますが、外す実行時にバグります。
returnキー押したらキーボード閉じる方法を教えてください。

Swift

1import UIKit 2 3class RankingPostViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate { 4 5 @IBOutlet weak var rankingPostTableView: UITableView! 6 var rankingPostInput:UITextField! 7 8 let sectionArr = ["選択してください", "入力してください(正式名称)"] 9 let placeholderArr = ["1位の漫画", "2位の漫画", "3位の漫画"] 10 11 @IBAction func closeRankingPostVC(_ sender: Any) { 12 self.dismiss(animated: true, completion: nil) 13 } 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 //rankingPostInput.delegate = self 18 self.rankingPostTableView.register(UINib(nibName: "RankingPostSelectCell", bundle: nil), forCellReuseIdentifier: "RankingPostSelectCell") 19 20 self.rankingPostTableView.register(UINib(nibName: "RankingPostInputCell", bundle: nil), forCellReuseIdentifier: "RankingPostInputCell") 21 22 // 空のセルの境界線を消す 23 rankingPostTableView.tableFooterView = UIView() 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 } 29 30 func textFieldShouldReturn(_ textField: UITextField) -> Bool{ 31 32 // キーボードを閉じる 33 rankingPostInput.resignFirstResponder() 34 35 return true 36 } 37 38 39 // セクションの数 40 func numberOfSections(in tableView: UITableView) -> Int { 41 return sectionArr.count 42 } 43 44 // セクションのタイトル 45 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 46 return sectionArr[section] 47 } 48 49 // tableViewのセルの数を返す 50 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 51 if section == 0 { 52 return 1 53 } 54 else if section == 1 { 55 return 3 56 } 57 else { 58 return 0 59 } 60 } 61 62 // tableViewのセルを返す 63 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 64 65 if indexPath.section == 0 { 66 let rankingPostSelectCell = tableView.dequeueReusableCell(withIdentifier: "RankingPostSelectCell", for: indexPath as IndexPath) as! RankingPostSelectCell 67 68 return rankingPostSelectCell 69 } 70 else if indexPath.section == 1 { 71 let rankingPostInputCell = tableView.dequeueReusableCell(withIdentifier: "RankingPostInputCell", for: indexPath as IndexPath) as! RankingPostInputCell 72 73 rankingPostInput = rankingPostInputCell.contentView.viewWithTag(1) as! UITextField 74 rankingPostInput.placeholder = placeholderArr[indexPath.row] 75 76 return rankingPostInputCell 77 } 78 else { 79 let rankingPostInputCell = tableView.dequeueReusableCell(withIdentifier: "RankingPostInputCell", for: indexPath as IndexPath) as! RankingPostInputCell 80 81 return rankingPostInputCell 82 } 83 } 84 85 // Cell が選択された場合 86 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 87 performSegue(withIdentifier: "toRankingPostCategoryViewController",sender: nil) 88 } 89 90} 91

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

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

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

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

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

fuzzball

2016/10/30 23:55

どうバグるのでしょうか?
guest

回答1

0

バグる件は、状況が分からないので置いておくとして、キーボードが閉じない件について。

rankingPostInputを共用しているので、最後に生成されたセル(のTextField)にしか反応しないと思います。(起動直後なら一番下のセル?)

とにかくキーボードが閉じればいい、という話であれば、resignFirstResponder()ではなく、endEditing()を使って下さい。

swift

1//rankingPostInput.resignFirstResponder() 2self.view.endEditing(true)

投稿2016/10/31 00:38

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問