質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

659閲覧

TableView セル選択で画面遷移後 戻ると順番がクリア

hirasho

総合スコア6

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/12/27 13:10

編集2019/12/27 21:16

閲覧ありがとうございます。
以前質問させて頂きサンプルコードで勉強させていただいております。

前提・実現したいこと

TableViewCell一覧から選択 → 画面遷移先で戻るボタン → TableViewCell一覧に戻りたい

上記を実現したいです。

@IBAction func unwindP(for unwindSegue: UIStoryboardSegue, towards subsequentVC: UIViewController) { }

現在はこちらを遷移元Viewに記述し戻ることは出来ています。

発生している問題

戻るボタンを押してTableViewCell一覧に戻ると、個別に用意してある別のCellが表示一覧に混じってしまいます。

遷移元ViewController
イメージ説明z

トマトを選択↓

遷移前
イメージ説明

tカスケード乳剤を選択↓

遷移先SecondView
イメージ説明

go backで戻ると↓

イメージ説明

該当のソースコード

遷移元ViewController

Swift

1import UIKit 2 3class ViewController: UITableViewController { 4 5 6 var getCell: Int! 7 8 var chosenCell: Int! = 0 9 10 let tomatoArray: [String] = ["tアニキ乳剤","tカスケード乳剤","tグレーシア乳剤","tコテツフロアブル","tトランスフォームフロアブル","tコルト顆粒水和剤","tスピノエース顆粒水和剤"] 11 12 let suikaArray: [String] = ["su農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 13 14 let ninzinArray: [String] = ["n農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 15 16 let rakkaseiArray: [String] = ["r農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 17 18 let zyagaArray: [String] = ["z農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 19 20 let aopapaiyaArray: [String] = ["a農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 21 22 let soramameArray: [String] = ["so農薬名1","農薬名2","農薬名3","農薬名4","農薬名5","農薬名6","農薬名7"] 23 24 var activeArray: [String] = [] 25 26 var tableViewCells: [String] = ["TableViewCellFirst", "TableViewCellSecond"] 27 28 @IBAction func unwindP(for unwindSegue: UIStoryboardSegue, towards subsequentVC: UIViewController) { 29 30 } 31 32 override func viewDidLoad() { 33 super.viewDidLoad() 34 35 //getCell = Int.random(in: 0...6) 36 37 self.tableView.delegate = self 38 self.tableView.dataSource = self 39 40 switch getCell { 41 case 0: 42 activeArray = tomatoArray 43 case 1: 44 activeArray = suikaArray 45 case 2: 46 activeArray = ninzinArray 47 case 3: 48 activeArray = rakkaseiArray 49 case 4: 50 activeArray = zyagaArray 51 case 5: 52 activeArray = aopapaiyaArray 53 case 6: 54 activeArray = soramameArray 55 default: 56 break 57 } 58 59 } 60 61 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 62 return activeArray.count 63 } 64 65 66 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 67 68 69 let getCellType = chosenCell 70 71 switch getCellType { 72 case 0: 73 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! TableViewCellFirst 74 cell = doSomethingWithTableViewCellFirst(cell: cell, index: indexPath.row) 75 return cell 76 case 1: 77 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TableViewCellSecond 78 cell = doSomethingWithTableViewCellSecond(cell: cell, index: indexPath.row) 79 return cell 80 case 2: 81 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell3", for: indexPath) as! TableViewCellThird 82 cell = doSomethingWithTableViewCellThird(cell: cell, index: indexPath.row) 83 return cell 84 case 3: 85 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell4", for: indexPath) as! TableViewCellForth 86 cell = doSomethingWithTableViewCellFourth(cell: cell, index: indexPath.row) 87 return cell 88 case 4: 89 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell5", for: indexPath) as! TableViewCellFifth 90 cell = doSomethingWithTableViewCellFifth(cell: cell, index: indexPath.row) 91 return cell 92 case 5: 93 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell6", for: indexPath) as! TableViewCellSixth 94 cell = doSomethingWithTableViewCellSixth(cell: cell, index: indexPath.row) 95 return cell 96 default: 97 var cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell7", for: indexPath) as! TableViewCellSeventh 98 cell = doSomethingWithTableViewCellSeventh(cell: cell, index: indexPath.row) 99 return cell 100 } 101 102 } 103 104 func doSomethingWithTableViewCellFirst(cell: TableViewCellFirst, index: Int) -> TableViewCellFirst { 105 cell.textLabel!.text = activeArray[index] 106 cell.mylabel!.text = "これは「TableViewCellFirst」です" 107 return cell 108 } 109 110 func doSomethingWithTableViewCellSecond(cell: TableViewCellSecond, index: Int) -> TableViewCellSecond { 111 cell.textLabel!.text = activeArray[index] 112 cell.mylabel!.text = "これは「TableViewCellSecond」です" 113 return cell 114 } 115 116 func doSomethingWithTableViewCellThird(cell: TableViewCellThird, index: Int) -> TableViewCellThird { 117 cell.textLabel!.text = activeArray[index] 118 cell.mylabel!.text = "これは「TableViewCellThird」です" 119 return cell 120 } 121 func doSomethingWithTableViewCellFourth(cell: TableViewCellForth, index: Int) -> TableViewCellForth { 122 cell.textLabel!.text = activeArray[index] 123 cell.mylabel!.text = "これは「TableViewCellForth」です" 124 return cell 125 } 126 func doSomethingWithTableViewCellFifth(cell: TableViewCellFifth, index: Int) -> TableViewCellFifth { 127 cell.textLabel!.text = activeArray[index] 128 cell.mylabel!.text = "これは「TableViewCellFifth」です" 129 return cell 130 } 131 func doSomethingWithTableViewCellSixth(cell: TableViewCellSixth, index: Int) -> TableViewCellSixth { 132 cell.textLabel!.text = activeArray[index] 133 cell.mylabel!.text = "これは「TableViewCellSixth」です" 134 return cell 135 } 136 func doSomethingWithTableViewCellSeventh(cell: TableViewCellSeventh, index: Int) -> TableViewCellSeventh { 137 cell.textLabel!.text = activeArray[index] 138 cell.mylabel!.text = "これは「TableViewCellSeventh」です" 139 return cell 140 } 141 142 143 144 145 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 146 147 chosenCell = indexPath.row 148 performSegue(withIdentifier: "showVedgi" , sender: nil) 149 150 } 151 152 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 153 154 let secondViewController = segue.destination as! SecondViewController 155 156 secondViewController.getCell = chosenCell 157 secondViewController.vedgiName = activeArray[chosenCell] 158 } 159 160 161 162} 163

遷移先SecondViewController

import UIKit class SecondViewController: UIViewController { var getCell: Int = 0 var vedgiName: String = "" @IBOutlet weak var localLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() print(getCell) } override func viewWillAppear(_ animated: Bool) { print(vedgiName) localLabel.text = vedgiName } }

試したこと

混じってしまうCellはIndexPathが関係して引っ張られてきていると思うのですが、どう対処すればよいかわかりません。
UserDefaultsでセルの選択状態を保存して再表示するのでしょうか?
調べたのですが、いまいち同じ状況がヒットしませんでした。

どなたかご教示頂けば幸いです。
どうぞよろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

weekplane

2019/12/27 21:08 編集

遷移をしない,元々の画面のスクショがあればわかりやすいかもです.(戻り画面VCのオリジナル)
hirasho

2019/12/27 21:06

weekplane 様 スクショを追加致しました。 ご確認よろしくお願い致します。
guest

回答2

0

ベストアンサー

ViewControllerの中にgetCellとchosenCellという2つのプロパティがあり、getCellはどの野菜(どの種類のセル)の農薬一覧を表示するのかを表し、chosenCellは表示中の農薬一覧の中で、どの農薬を選択したかを表しているものだと思いますが、cellForRowAtのdelegateメソッドの中でchosenCellの値を元に表示する農薬一覧(表示するセルの種類)を決めているのが誤っていると思います。

「tカスケード乳剤」を選択したらchosenCellの値は1になりますから、画面を戻った時に既に表示されているセル(TableViewCellFirst)はそのまま表示されたままですが、下にスクロールして続きのセルを表示するとchosenCellの値が1になっているためTableViewCellSecondのセルが表示されてしまうと思います。

cellForRowAtのdelegateメソッドの中で、chosenCellではなく、getCellの値を元に表示するセルを決めればうまくいきそうに思います。

投稿2019/12/27 23:50

編集2019/12/28 01:51
TakeOne

総合スコア6299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hirasho

2019/12/28 12:42

TakeOne様 ご回答ありがとうございます。 ご指摘通り、choseCellをgetCellに変更したところ理想通り動作しました!! 「「tカスケード乳剤」を選択したらchosenCellの値は1になりますから、画面を戻った時に既に表示されているセル(TableViewCellFirst)はそのまま表示されたままですが、下にスクロールして続きのセルを表示するとchosenCellの値が1になっているためTableViewCellSecondのセルが表示されてしまう」 セル自体がdequeueReusableCellで再利用されているため、元のセルは表示されたままで上下にスクロールした際に別のセルがそこで初めて表示されてしまっていたのですね。理解できてスッキリしました。 貴重なお時間をいただきまして、誠にありがとうございました。
guest

0

遷移先VCでgo backボタンを押したら元の画面に戻るように,
遷移先VCに

VC2

1@IBAction func 関数名(_ sender: Any) { 2 dismiss(animated: true, completion: nil) 3 } 4

を追加するのは試しましたか?(※go backボタンにoutket接続をする)

投稿2019/12/27 21:17

weekplane

総合スコア29

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hirasho

2019/12/27 21:49

ご回答ありがとうございます。 dismissメソッドは試したのですが、遷移元VCまで二段階戻ってしまいます。 アニキ乳剤、カスケード乳剤...と表示されている一覧に戻りつつ、順番がバラバラにならない方法を知りたいです。判りづらい質問で大変申し訳ありません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問