前提・実現したいこと
ボタンを押すと記録が次の画面であるtableView上に保存されるようにしたいのですがtableViewに何も表示されません。
該当のソースコード
ResultViewController.swift
swift
1@IBAction func record(_ sender: Any) { 2 3 //配列を取り出す 4 if (UserDefaults.standard.object(forKey: "record") != nil) { 5 array = UserDefaults.standard.object(forKey: "record") as! [String] 6 } 7 8 array.append(countLabel2.text!) 9 UserDefaults.standard.set(array, forKey: "record") 10 }
SaveViewController.swift
Swift
1mport UIKit 2 3class SaveViewController: UIViewController,UITableViewDelegate, 4UITableViewDataSource { 5 6 @IBOutlet var tableView: UITableView! 7 8 var resultArray = [String]() 9 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 tableView.delegate = self 15 tableView.dataSource = self 16 17 18 19 20 21 } 22 23 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 return resultArray.count 26 } 27 28 func numberOfSections(in tableView: UITableView) -> Int { 29 return 1 30 } 31 32 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 33 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 34 35 cell.textLabel?.text = resultArray[indexPath.row] 36 37 return cell 38 } 39 40 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 41 return 60 42 } 43 44 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 45 46 if editingStyle == .delete { 47 resultArray.remove(at: indexPath.row) 48 49 UserDefaults.standard.set(resultArray, forKey: "todo") 50 51 tableView.reloadData() 52 } 53 } 54 55 /* 56 // MARK: - Navigation 57 58 // In a storyboard-based application, you will often want to do a little preparation before navigation 59 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 60 // Get the new view controller using segue.destination. 61 // Pass the selected object to the new view controller. 62 } 63 */ 64 65} 66
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/28 07:30
2018/12/29 15:41
2018/12/30 03:34