前提・実現したいこと
TableViewに登録されたデータがdeleteして消したはずなのですが、再起動すると元に戻ってしまいます。
コードを調べても間違いが分からないので解決策を教えていただきたいです。
edithingstyleの部分がそうです。
該当のソースコード
Swift
1import UIKit 2 3class SaveViewController: UIViewController,UITableViewDelegate, 4UITableViewDataSource { 5 6 @IBOutlet var tableView: UITableView! 7 8 var resultArray = [String]() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 tableView.delegate = self 14 tableView.dataSource = self 15 16 } 17 18 19 override func viewWillAppear(_ animated: Bool) { 20 super.viewDidAppear(animated) 21 22 if UserDefaults.standard.object(forKey: "save") != nil { 23 resultArray = UserDefaults.standard.object(forKey: "save") as! [String] 24 25 } 26 tableView.reloadData() 27 28 } 29 30 31 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 32 return resultArray.count 33 } 34 35 func numberOfSections(in tableView: UITableView) -> Int { 36 return 1 37 } 38 39 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 40 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 41 42 cell.textLabel!.text = resultArray[indexPath.row] 43 44 return cell 45 } 46 47 /*func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 48 return 40 49 }*/ 50 51 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 52 53 if editingStyle == .delete { 54 resultArray.remove(at: indexPath.row) 55 56 UserDefaults.standard.set(resultArray, forKey: "todo") 57 58 tableView.reloadData() 59 } 60 } 61
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。