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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

790閲覧

Table cell上にあるボタンのインデックスを、TableViewController上で宣言している変数に受け渡したい。

tasmicsy

総合スコア13

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2021/11/29 08:38

編集2021/11/30 01:51

やりたい事

下図にある通り、各質問(q.question[index.row]に対して5個の◯ボタンを配置し、選択されている●の値をq.answer[indexPath.row]に格納したいです。
◯ボタンが選択されて●になる毎に、QuizCellクラス内のcurrentQuizButtonIndexは更新されるようにはなっていますが、それをTableViewControllerにて宣言されている変数qの中に入れる方法がわかりません。
イメージ説明

特にわからないこと

TableViewCellで、対応するindexPath.rowを用いたいのですが、Cell.swiftでは使うことはできないのでしょうか。
何問目のどのボタンが押されたかをセーブしたいです。

各ソースコード

QuizCell

Swift

1import UIKit 2import Foundation 3 4protocol QuizCellDelegate { 5 func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int) 6} 7 8class QuizCell: UITableViewCell { 9 var currentQuizButtonIndex: Int = 0 { 10 didSet { 11 let value = self.currentQuizButtonIndex 12 self.updateCurrentQuizButton(value) 13 if let delegate = self.delegate { 14 delegate.quizCellDidChangeCurrentButtonIndex(self, index: value) 15 } 16 } 17 } 18 @IBOutlet weak var questionLabel: UILabel! 19 20 @IBOutlet var answerButtons: [UIButton]! 21 22 var delegate: QuizCellDelegate? 23 24 override func awakeFromNib() { 25 super.awakeFromNib() 26 //print("ここまできてるか確認") 27 // Initialization code 28 } 29 30 @IBAction func didTapQuizButton(_ sender: UIButton) { 31 if let index = self.answerButtons.firstIndex(of: sender){ 32 self.currentQuizButtonIndex = index 33 delegate?.quizCellDidChangeCurrentButtonIndex(self, index: index) 34 print(index) 35 } 36 } 37 38 private func updateCurrentQuizButton(_ currentIndex: Int){ 39 for (index, answerButton) in self.answerButtons.enumerated(){ 40 if index == currentIndex { 41 answerButton.setTitle("●", for: .normal) 42 } else { 43 answerButton.setTitle("○", for: .normal) 44 45 } 46 } 47 } 48 49 50 override func setSelected(_ selected: Bool, animated: Bool) { 51 super.setSelected(selected, animated: animated) 52 53 // Configure the view for the selected state 54 } 55 56} 57

ViewControllerコード

Swift

1 2import UIKit 3 4class AnswerQuizViewController: UIViewController, UITableViewDelegate { 5 6 7 var q: QuestionSeries! 8 9 @IBOutlet weak var quizTableView: UITableView! 10 11 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 quizTableView.dataSource = self 16 quizTableView.delegate = self 17 18 // cell xibファイルを使うときは書く必要があるやつ。 19// quizTableView.register(UINib(nibName: K.Cells.QuizCellNibName, bundle: nil), forCellReuseIdentifier: K.Cells.QuizCellIdentifier) 20 quizTableView.register(UINib(nibName: "QuizCell", bundle: nil), forCellReuseIdentifier: "QuizCellIdentifier") 21 22 // Do any additional setup after loading the view. 23 } 24 25 26 27 28 // MARK: - Navigation 29 30 // In a storyboard-based application, you will often want to do a little preparation before navigation 31// override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 32// // Get the new view controller using segue.destination. 33// // Pass the selected object to the new view controller. 34//// if segue.identifier == K.Segue.checkResult { 35//// let resultViewController = segue.destination as! ResultViewController 36//// answerQuizViewController.q = 37//// print(answerQuizViewController.q) 38// } 39 40 41} 42 43// MARK: - quizTableViewのアレンジ 44 45extension AnswerQuizViewController: UITableViewDataSource, QuizCellDelegate { 46 func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int) { 47 if let indexPath = self.quizTableView.indexPath(for: cell){ 48 self.q.question[indexPath.row].answer = index 49 print(index) 50 }else{ 51 print("ここきてます") 52 } 53 } 54 55 56func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 57 return q.question.count 58 //print(q.question.count) 59} 60 61func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 62 let question = q.question[indexPath.row] 63 let cell = quizTableView.dequeueReusableCell(withIdentifier: K.Cells.QuizCellIdentifier, for: indexPath) as! QuizCell 64 cell.questionLabel.text = question.text 65 // print(question.text) 66 67 return cell 68} 69} 70

また、もし他の方法で実装できるなどご助言あれば、ぜひご教授いただけますと幸いです。
よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

コードを見た感じ、基本的にこのコードでできそうに見えますが、1つできていないのが、 cellForRowAt のdelegateメソッドの中で、作成したQuizCellに対してdelegateを設定していないことだと思います。

cellForRowAt のdelegateメソッドの中に次のコードが必要だと思います。

swift

1cell.delegate = self

基本的な仕組みはできているのに、これを設定していないので、

delegate?.quizCellDidChangeCurrentButtonIndex(self, index: index)

delegateがnilのためquizCellDidChangeCurrentButtonIndexが呼びだされないということのように見えます。

delegateの値がどうなっているかや、どのメソッドがどこまで呼ばれているか、printを入れる等して動作状況をよく確認することをお勧めします。

なお、quizCellDidChangeCurrentButtonIndexの呼び出しが
currentQuizButtonIndexdidSetの中とdidTapQuizButtonメソッドの中の2箇所にあるので、どちらか一方にしないと2回呼び出されてしまうように見えます。

投稿2021/11/30 07:57

TakeOne

総合スコア6299

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

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

tasmicsy

2021/11/30 08:42

ご丁寧かつ的確な回答、ありがとうございます。 おっしゃる通りに記載したところ、 'delegate?.quizCellDidChangeCurrentButtonIndex(self, index: index)' が正常に動作しました。 'func quizCellDidChangeCurrentButtonIndex()'内部に記載しているprintが動作せず、首を傾げておりました(質問文にその旨記載すべきでした。。不思議に思ったところに原因がある可能性が高いということを学びました。) delegateがnilであることが原因だったのですね。delegateに何か問題があるかとは浮かびつつ、delegateの値を確認しておりませんでした。 大変参考になりました。どうもありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問