###前提・実現したいこと
プログラミング初心者で今swiftの勉強のために簡単なtodoアプリを作っています。変なUIかもしれませんが、下記がイメージ図です。(tableviewにtodoを表示させるために、遷移先画面のTextFieldでtodoを入力)
補足:左の画面(class: ViewController) 右の画面(class: TodoViewController)
tableViewCell(identifier: todoCell)
segue(identifier: goSetting)
ここに質問したいことを詳細に書いてください
TextFieldで入力した値をTableViewに表示させようとしている最中なのですが、中々できず
エラーが出てしまいました。
このエラーはどう解消されるのでしょうか?
またお時間があれば、どうコードを書き換えれば、TextFieldに入力した値がTableViewに表示されるのかも教えていただけますと幸いです。
###発生している問題・エラーメッセージ
① fatal error: Index out of range ② cellが表示されない ③ TextFieldで入力した値を前の画面に戻った際に、cellに表示できない
###該当のソースコード // // ViewController.swift // todo2 segue // // Created by ○○ on 2017/01/23. // Copyright © 2017年 △△ . All rights reserved. // //1画面目の方のコードです import UIKit class ViewController: UIViewController, UITableViewDataSource { //戻ってきた時に値を受ける var word: [String?] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var tableView: UITableView! @IBAction func plus(_ sender: Any) { performSegue(withIdentifier: "goSetting", sender: nil) } //Cellの総数を返すdatasourceメソッド、必ず記述する必要があります func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableview: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } //Cellに値を設定するdatasourceメソッド。必ず記述する必要があります func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //今回表示を行う、cellオブジェクト(1行)を取得する let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell" , for: indexPath) cell.textLabel?.text = word[indexPath.row] return cell } } // // TodoViewController.swift // todo2 segue // // Created by ○○ on 2017/01/23. // Copyright © 2017年 △△. All rights reserved. // //2画面目の方のコードです import UIKit class TodoViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. textField.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ @IBOutlet weak var textField: UITextField! //デリゲートメソッド func textFieldShouldReturn(_ textField: UITextField) -> Bool { //キーボードを閉じる textField.resignFirstResponder() //入力値を受け取る let keyword : [String?] = [textField.text] func prepare(for segue: UIStoryboardSegue, sender: Any?) { //次の画面のインスタンスを取得 let nextViewController = segue.destination as! ViewController //次の画面のインスタンスに取得した値を渡す //ここは.textじゃなくて受け入れる側で用意した箱(変数)の名前 nextViewController.word = keyword } //Bool値を返す return true } }
###試したこと
書籍やwebを参考にやってみたのですが...
どこも配列の中身をtableviewに表示させるというものばかりで、
textfieldに入力した値を随時表示させていくというものが見当たりませんでした
###補足情報(言語/FW/ツール等のバージョンなど)
swift3.0
xcode8.2.1
macOS Sierra(バージョン 10.12)
汚いコードで申し訳ございませんが、ご教授いただけますと幸いです。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/24 15:33