前提・実現したいこと
swiftでsafariのブックマークのような,tableviewのcellをタップするとそこからさらに下の階層のtableviewに遷移できるような機能を実装したいと考えています.
tableviewのcellからtableviewへの遷移と,それぞれのtableのcellの追加・削除はできるようになっていますが,遷移先から戻り再び遷移するとtableviewに格納する配列の値が初期化されてしまいます.
これを初期化せずに保存し,viewControllerのtableviewのcellの増減に合わせて内包するviewも増減するようにしたいと考えています.
storyboardは以下のように配置してあります.
該当のソースコード
以下に遷移先のProductViewControllerのソースコードを示します.
また遷移前のviewcontrollerは,下記のソースコードの変数,クラス名,関数名,編集に関するコメントアウトがされていない以外は同じものとなっています.
ProductViewController
1import UIKit 2 3class ProductViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 var productTitle:[String] = [] 6 7 @IBOutlet weak var productTable: UITableView! 8 @IBOutlet weak var addButton: UIBarButtonItem! 9 10 //ここに戻ってくる時の処理 11 @IBAction func GoToProductViewController(_ segue : UIStoryboardSegue){ 12 guard let source = segue.source as? AddProductViewController else { 13 fatalError() 14 } 15 if let text = source.productName!.text { 16 productTitle.append(text) 17 productTable.reloadData() 18 } 19 20 } 21 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 // Do any additional setup after loading the view, typically from a nib. 25 productTable.delegate = self 26 productTable.dataSource = self 27 28 //セルの編集buttonの追加 29 //self.navigationController?.isNavigationBarHidden = false 30 //navigationItem.rightBarButtonItem = editButtonItem 31 //productTable.reloadData() 32 } 33 34 // Table Viewにいくつのセルを表示するかを指定する 35 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 36 return productTitle.count 37 } 38 39 // セルに表示する値を設定する 40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 41 // データを表示するセルを取得する 42 let productCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) 43 44 // セルに表示する値を設定する 45 productCell.textLabel!.text = productTitle[indexPath.row] 46 47 // データを設定したセルを返却する 48 return productCell 49 } 50 //セルの編集 51 //override func setEditing(_ editing: Bool, animated: Bool) { 52 //override前の処理を継続してさせる 53 //super.setEditing(editing, animated: animated) 54 //productTable.isEditing = editing 55 56 //addButton有効化,無効化 57 //if editing { 58 //addButton.isEnabled = true 59 //self.navigationItem.hidesBackButton = false 60 //} 61 //else{ 62 //addButton.isEnabled = false 63 //self.navigationItem.hidesBackButton = true 64 //} 65 //} 66 67 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 68 return true 69 } 70 71 //編集ボタンが押下された時の処理 72 //func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 73 74 //dataを消してから 75 //productTitle.remove(at: indexPath.row) 76 //tableViewCellの削除 77 //tableView.deleteRows(at: [indexPath], with: .automatic) 78 //productTable.reloadData() 79 //} 80 81 override func didReceiveMemoryWarning() { 82 super.didReceiveMemoryWarning() 83 // Dispose of any resources that can be recreated. 84 } 85} 86
補足情報(FW/ツールのバージョンなど)
swift 4.2 xcode 10.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。