ToDOアプリでToDoListが表示されない
swift4でToDoアプリを作成しているのですが、Add画面でToDoリストに内容を追加したのですが、ToDoListに追加した内容が表示されませんので、アドバイスを頂きたいです。
発生している問題・エラーメッセージ
ToDoListに追加した内容が表示されない
該当のソースコード
swift
1-ViewController.swift- 2import UIKit 3 4class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 if UserDefaults.standard.object(forKey: "TodoList") != nil { 12 todoItem = UserDefaults.standard.object(forKey: "TodoList") as! [String] 13 } 14 } 15 16 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 return todoItem.count 18 } 19 20 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 //変数を作る 22 let TodoCell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TodoCell", for: indexPath) 23 //変数の中身を作る 24 TodoCell.textLabel!.text = todoItem[indexPath.row] 25 //戻り値の設定(表示する中身) 26 return TodoCell 27 } 28 29 30 31 32 override func didReceiveMemoryWarning() { 33 super.didReceiveMemoryWarning() 34 // Dispose of any resources that can be recreated. 35 } 36 37 38} 39
swift
1-AddController.swift- 2import UIKit 3 4var todoItem = [String]() 5 6class AddController: UIViewController { 7 8 @IBOutlet weak var TodoTextField: UITextField! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 // Do any additional setup after loading the view. 14 } 15 16 @IBAction func TodoAddButton(_ sender: Any) { 17 todoItem.append(TodoTextField.text!) 18 TodoTextField.text = "" 19 //変数の中身をUDに追加 20 UserDefaults.standard.set(todoItem, forKey: "TodoList" ) 21 } 22 23 24 25 override func didReceiveMemoryWarning() { 26 super.didReceiveMemoryWarning() 27 // Dispose of any resources that can be recreated. 28 } 29 30 31 /* 32 // MARK: - Navigation 33 34 // In a storyboard-based application, you will often want to do a little preparation before navigation 35 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 36 // Get the new view controller using segue.destinationViewController. 37 // Pass the selected object to the new view controller. 38 } 39 */ 40 41}
補足情報(FW/ツールのバージョンなど)
Xcode ver.9.4.1
swift4
Main.storyboardの画面
【コンソール画面に表示される内容】
![]
【TodoAddButton】
【20180926時点でのソースコード】
回答3件
あなたの回答
tips
プレビュー