前提・実現したいこと
学習の為、ToDoアプリを作っているのですが、TableViewのCellにチェックマークをつけれる機能の実装を行いたいです。
発生している問題・エラーメッセージ
チェックマークを付ける、外す機能は実装出来たのですが、チェックマークを付けた状態で画面遷移を行うとチェックマークが消えてしまいます。
該当のソースコード
ViewController.swift import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! // チェックマークの処理 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let Cell = tableView.cellForRow(at: indexPath) Cell?.accessoryType = .checkmark } // チェックマークの処理 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { let Cell = tableView.cellForRow(at: indexPath) Cell?.accessoryType = .none } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return todobox.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let ToDoCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ToDoCell", for: indexPath) ToDoCell.textLabel!.text = todobox[indexPath.row] return ToDoCell } override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.allowsMultipleSelection = true if UserDefaults.standard.object(forKey: "ToDoList") != nil { todobox = UserDefaults.standard.object(forKey: "ToDoList") as! [String] } } }
AddViewController.swift import UIKit var todobox = [String]() class AddViewController: UIViewController { // テキストフィールド @IBOutlet weak var textField: UITextField! // 追加ボタン @IBAction func addButton(_ sender: Any) { todobox.append(textField.text!) textField.text = "" UserDefaults.standard.set(todobox, forKey: "ToDoList") } override func viewDidLoad() { super.viewDidLoad() } }
試したこと
チェックマークの情報をUserDefaultsを使って保存すると解決するかもしれない事は分かり、試しており
調べているのですが、解決には至っていません。
ご教示頂けると幸いです。
宜しくお願いします。
補足情報(FW/ツールのバージョンなど)
Swift4 Xcode10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/04 14:23
2018/12/04 14:58
2018/12/06 12:46
2018/12/06 12:52
2018/12/06 13:05
2018/12/06 13:08
2018/12/06 13:24