前提・実現したいこと
簡易的なToDoアプリを制作しています。下記を実現したく、色々と調べてみたのですが一部解決できません。
1.一覧画面上でtextViewにテキストを入力
2.tableViewCellに入力されたテキストが表示(UserDefalutsで保存)
3.入力済みのtableViewCellをタップ
4.編集画面(textView)に値を保持した状態で遷移
5.内容を編集後、編集完了ボタンタップで一覧画面に戻る
6.tabeViewCellが編集した内容に更新される
上記手順のうち、6が実装できないため、解決策をご教示いただけませんでしょうか。
よろしくお願いいたします。
該当のソースコード
ViewController
1import UIKit 2import AVFoundation 3 4class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 5 6 var todoList = [String]() 7 8 var talker = AVSpeechSynthesizer() 9 10 var senderText: String? 11 12 @IBOutlet weak var inputTextView: UITextView! 13 14 @IBOutlet weak var tableView: UITableView! 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 let userDefaults = UserDefaults.standard 20 if let storedTodoList = userDefaults.array(forKey: "saveTodo") as? [String] { 21 todoList.append(contentsOf: storedTodoList) 22 } 23 24 } 25 26 override func viewWillAppear(_ animated: Bool) { 27 super.viewWillAppear(animated) 28 29 tableView.reloadData() 30 } 31 32 @IBAction func tapAddButton(_ sender: Any) { 33 if let inputText = inputTextView.text { 34 self.todoList.insert(inputText, at: 0) 35 self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableView.RowAnimation.right) 36 } 37 38 inputTextView.endEditing(true) 39 inputTextView.text = "" 40 41 let userDefaults = UserDefaults.standard 42 userDefaults.set(self.todoList, forKey: "saveTodo") 43 userDefaults.synchronize() 44 45 print("save") 46 } 47 48 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 49 self.view.endEditing(true) 50 } 51 52 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 53 return todoList.count 54 } 55 56 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 57 58 59 let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell", for: indexPath) 60 let todoText = todoList[indexPath.row] 61 cell.textLabel?.text = todoText 62 63 64 cell.textLabel?.numberOfLines = 0 65 cell.accessoryType = .detailButton 66 67 return cell 68 } 69 70 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 71 if editingStyle == UITableViewCell.EditingStyle.delete { 72 todoList.remove(at: indexPath.row) 73 tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.fade) 74 let userDefaults = UserDefaults.standard 75 userDefaults.set(self.todoList, forKey: "saveTodo") 76 userDefaults.synchronize() 77 78 print("delete") 79 } 80 } 81 82 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 83 tableView.deselectRow(at: indexPath, animated: false) 84 performSegue(withIdentifier: "goEdit", sender: indexPath) 85 senderText = todoList[indexPath.row] 86 87 print(todoList[indexPath.row]) 88 } 89 90 func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 91 senderText = todoList[indexPath.row] 92 } 93 94 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 95 let secondVC = segue.destination as! SecondViewController 96 secondVC.receivedText = senderText 97 } 98 99 func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { 100 let utterance = AVSpeechUtterance(string: todoList[indexPath.row]) 101 utterance.voice = AVSpeechSynthesisVoice(language: "en-US") 102 utterance.preUtteranceDelay = 0; 103 self.talker.speak(utterance) 104 print("accessoryTapped") 105 } 106}
SecondViewController
1import UIKit 2 3class SecondViewController: UIViewController { 4 5 var receivedText: String? 6 7 @IBOutlet weak var editngTextView: UITextView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 editngTextView.text = receivedText 13 } 14 15 16 17 @IBAction func tapOKButton(_ sender: Any) { 18 19 } 20}
回答2件
あなたの回答
tips
プレビュー