追記 1/9
himeji001さんのアドバイス通りやってみたところ
Array1には回数の入ったresultArray1と日付が入ったdateArray1を入れることが出来ました。
しかしSaveViewControllerのViewDidLoadで,取り出しArray2に代入して、cellForRowAtにresultArray2とdateArray2のIndexPath.rowを表示させることがまだ出来ません。(Thread 1: Fatal error: Index out of range)
そもそも、Array2 = [resultArray2, dateArray2]なのでArray2[IndexPath.row]はわかりますが、その中の配列resultArray2、dateArray2に[IndexPath.row]は使えないですよね。それぞれ新しく更新されたデータをcellForRowAtで
cell.textLabel!.text (resultArray2の値)
cell.detailTextLabel!.text(dateArray2の値)
を入れる方法はありますでしょうか。
あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
そしてスッキリした分かりやすいコードを書くために些細なことでもいいのでアドバイスいただけたら幸いです。
ResultViewController.swift
Swift
1 2var resultArray1 = [String]() 3 4 var dateArray1 = [String]() 5 6 var Array1: [[String]] = [] 7 8 var date = Date() 9 10 11... 12... 13... 14 15@IBAction func record(_ sender: Any) { 16 17 //配列を取り出す 18 if (UserDefaults.standard.object(forKey: "save") != nil) { 19 Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]] 20 21 } 22 23 24 //zikanを呼び出す 25 zikan() 26 //resultArrayに回数を追加 27 resultArray1.append(countLabel2.text!) 28 29 Array1 = [resultArray1, dateArray1] 30 31 UserDefaults.standard.set(Array1, forKey: "save") 32 33 print(Array1.description) 34 print(resultArray1.description) 35 print(dateArray1.description) 36 } 37 38 39 func zikan() { 40 //日付のフォーマットを指定する 41 let format = DateFormatter() 42 format.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMdkHm", options: 0, locale: Locale(identifier: "ja_JP")) 43 44 //日付をStringに変換する 45 let sDate = format.string(from: date) 46 //日付をdateArrayに入れる 47 dateArray1.append(sDate) 48 }
SaveViewController.swift
swift
1var resultArray2 = [String]() 2 3 var dateArray2 = [String]() 4 5 var Array2: [[String]] = [] 6 7... 8... 9... 10 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 tableView.delegate = self 16 tableView.dataSource = self 17 18 //Array2にresultArray2とdateArray2を代入 19 Array2 = [resultArray2, dateArray2] 20 21 if UserDefaults.standard.object(forKey: "save") != nil { 22 23 Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]] 24 } 25 26 print(resultArray2.description) 27 print(dateArray2.description) 28 print("Array2:", Array2.description) 29 30 31 } 32 33 34 override func viewWillAppear(_ animated: Bool) { 35 super.viewDidAppear(animated) 36 37 38 39 tableView.reloadData() 40 41 42 } 43 44 45 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 46 return Array2.count 47 } 48 49 func numberOfSections(in tableView: UITableView) -> Int { 50 return 1 51 } 52 53 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 54 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 55 56 cell.textLabel!.text = resultArray2[indexPath.row] 57 cell.detailTextLabel!.text = dateArray2[indexPath.row] 58 59 return cell 60 } 61 62 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 63 return 40 64 } 65 66 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 67 68 if editingStyle == .delete { 69 Array2.remove(at: indexPath.row) 70 71 UserDefaults.standard.set(Array2, forKey: "save") 72 73 tableView.reloadData() 74 } 75 } 76
・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
追記1/9
Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]のところにThread 1: signal SIGABRTが発生する理由がわかりません。不必要なキャッシュは切ってあります。
swift
1 2var resultArray2 = [String]() 3 4 var dateArray2 = [String]() 5 6 var Array2: [[String]] = [] 7 8 9... 10... 11... 12 13 14 15override func viewDidLoad() { 16 super.viewDidLoad() 17 18 tableView.delegate = self 19 tableView.dataSource = self 20 21 if UserDefaults.standard.object(forKey: "save") != nil { 22 23 Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]] 24 } 25 26 27 self.resultArray2 = self.Array2[0] 28 self.dateArray2 = self.Array2[1] 29 30 Array2 = [resultArray2, dateArray2] 31 32 33 //Array2 = [ ["晴れ", "雨", "雪"], 34 //["2019/1/1", "2019/1/2", "2019/1/3"] ] 35 36 print("Array2:", Array2.description) 37 print(resultArray2.description) 38 print(dateArray2.description) 39 40 } 41 42 43 override func viewWillAppear(_ animated: Bool) { 44 super.viewDidAppear(animated) 45 46 tableView.reloadData() 47 } 48 49 50 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 51 52 return resultArray2.count 53 54 } 55 56 func numberOfSections(in tableView: UITableView) -> Int { 57 return 1 58 } 59 60 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 62 63 64 print("Array2:at cellForRowAt", Array2.description) 65 print(Array2.count) 66 print(resultArray2.description) 67 print(dateArray2.description) 68 69 cell.textLabel!.text = resultArray2[indexPath.row] 70 cell.detailTextLabel!.text = dateArray2[indexPath.row] 71 72 return cell 73 } 74 75 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 76 return 40 77 } 78 79 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 80 81 if editingStyle == .delete { 82 Array2.remove(at: indexPath.row) 83 84 UserDefaults.standard.set(Array2, forKey: "save") 85 86 tableView.reloadData() 87 } 88 } 89
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/07 05:20
2019/01/07 05:55
2019/01/07 08:44
2019/01/07 08:52
2019/01/07 19:49
2019/01/07 23:45 編集
2019/01/08 03:17
2019/01/08 04:03 編集
2019/01/08 07:01
2019/01/08 09:31