前提・実現したいこと
メモアプリを作成していて作成したメモをSearchBarで検索出来る機能を作成しているのですが、
SearchBarで検索した検索結果のCellをタップして、文字を編集して保存をするとメモ一覧に戻るとCellが置き換わってしまいます。
具体的にはメモ一覧のCell3番目を検索して検索結果から編集してメモ一覧に戻るとCellの3番目が編集したテキストに変わっていて欲しいのですが、Cell3番目は編集前のテキストのままで、Cell1番目が元々あった別のテキストからCell3番目の編集したテキストに置き換わってしまっている状況です。
該当のソースコード
import UIKit // textViewに入力された文字を格納する変数 var memoText = [String]() var honIndex = -1 class ViewController: UIViewController { // 編集のために全画面から値を受け取る var aDate: String = "" @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() print("vc:",aDate) // 編集のために設定 textView.text = aDate } @IBAction func setButton(_ sender: Any) { // -1は新規作成なので追加、それ以外はmemoTextを書き換え if honIndex == -1 { memoText.append(textView.text!) print("追加") } else { memoText[honIndex] = textView.text! print("再編集") } // memoTextをUserDefaultsで保存する UserDefaults.standard.set(memoText, forKey: "memoTextKey") } }
import UIKit // 検索結果を格納 var searchResult = [String]() class TableViewController: UITableViewController,UISearchBarDelegate { @IBOutlet weak var searchBar: UISearchBar! override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self if UserDefaults.standard.object(forKey: "memoTextKey") != nil { memoText = UserDefaults.standard.object(forKey: "memoTextKey") as! [String] } } override func viewDidAppear(_ animated: Bool) { // TableView更新 tableView.reloadData() // 前画面から戻ってきたらhonIndexを初期化 honIndex = -1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // SearchBarにテキストが入力されている場合は検索結果をTableViewに表示、入力されていない場合はメモ一覧(memoText)を表示する if searchBar.text != "" { return searchResult.count } else { return memoText.count } } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // cellの表示内容を設定 let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) if searchBar.text != "" { cell.textLabel!.text = "(searchResult[indexPath.row])" } else { cell.textLabel!.text = memoText[indexPath.row] } return cell } // Cellを押した時の処理 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // honIndexに押されたCell番号を格納する honIndex = indexPath.row performSegue(withIdentifier: "Segue", sender: nil) } // 予定削除機能 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { let index = indexPath.row memoText.remove(at: index) // 予定を削除した事を保存する UserDefaults.standard.set(memoText, forKey: "memoTextKey") tableView.reloadData() } // 検索ボタンを押した時に呼ばれる func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { self.view.endEditing(true) searchBar.showsCancelButton = true searchResult = memoText.filter { $0.lowercased().contains(searchBar.text!.lowercased()) } self.tableView.reloadData() } // キャンセルボタンを押した時に呼ばれる func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchBar.showsCancelButton = false self.view.endEditing(true) searchBar.text = "" tableView.reloadData() } // 入力開始前に呼ばれる func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { searchBar.showsCancelButton = true return true } // ViewControllerへの引き継ぎ設定 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! ViewController // 新規以外 if(honIndex != -1) { vc.aDate = memoText[honIndex] print("tvc:", vc.aDate) } // searchBarにtextが入力されている場合searchResultを引き継ぐ if searchBar.text != "" { vc.aDate = searchResult[honIndex] } } }
試したこと
検索結果のCellをタップすると出力にTableViewControllerがメモ一覧のCell1番目をタップしていることになっていて、ViewControllerが検索結果のCellのテキストが表示されている状態です。
このことから推測ですが、SearchBarの検索結果のCellがメモ一覧のCellをタップしていることになっていて、SearchBarの検索結果を更新させたら良いのだろうか?と考え、その方向から解決策を探しているものの解決していない状況です。
ご教示頂けると幸いです。
TableViewを更新させれば良いのかと思い、
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
の
if searchBar.text != "" { cell.textLabel!.text = "(searchResult[indexPath.row])" tableView.reloadData()
tableVeiew.reloadData()を追加して更新しようと試しましたが、出来ませんでした。
補足情報(FW/ツールのバージョンなど)
Xcode10
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/06 14:00