写真のデータを配列をEditingStyle削除するようにコードを書いたのですが、この画面上からは消えても、再度アプリを立ち上げるとデータがこのように保存されたままになってしまいます。画像の選択したデータだけを削除する方法はありますでしょうか。Array2 = [resultArray2, dateArray2]なので[indexpath.row]は使えないと思うのですが、何かアドバイスでも頂けたら幸いです。
swift
1var resultArray2 = [String]() 2 3 var dateArray2 = [String]() 4 5 var date2 = Date() 6 7 var Array2: [[String]] = [] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 13 tableView.delegate = self 14 tableView.dataSource = self 15 16 if UserDefaults.standard.object(forKey: "save") != nil { 17 18 Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]] 19 } 20 21 22 self.resultArray2 = self.Array2[0] 23 self.dateArray2 = self.Array2[1] 24 25 Array2 = [resultArray2, dateArray2] 26 27 print("Array2:", Array2.description) 28 29 30 } 31 32 33 override func viewWillAppear(_ animated: Bool) { 34 super.viewDidAppear(animated) 35 36 tableView.reloadData() 37 } 38 39 40 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 41 42 return resultArray2.count 43 } 44 45 func numberOfSections(in tableView: UITableView) -> Int { 46 return 1 47 } 48 49 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 50 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 51 52 53 /*print("Array2:at cellForRowAt", Array2.description) 54 print(Array2.count) 55 print(resultArray2.description) 56 print(dateArray2.description)*/ 57 58 cell.textLabel!.text = resultArray2[indexPath.row] 59 cell.detailTextLabel!.text = dateArray2[indexPath.row] 60 61 return cell 62 } 63 64 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 65 return 40 66 } 67 68 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 69 70 if editingStyle == .delete { 71 resultArray2.remove(at: indexPath.row) 72 dateArray2.remove(at: indexPath.row) 73 74 75 UserDefaults.standard.set(Array2, forKey: "save") 76 77 tableView.reloadData() 78 79 } 80 } 81
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/11 18:16