やりたい事
下図にある通り、各質問(q.question[index.row]に対して5個の◯ボタンを配置し、選択されている●の値をq.answer[indexPath.row]に格納したいです。
◯ボタンが選択されて●になる毎に、QuizCellクラス内のcurrentQuizButtonIndexは更新されるようにはなっていますが、それをTableViewControllerにて宣言されている変数qの中に入れる方法がわかりません。
特にわからないこと
TableViewCellで、対応するindexPath.rowを用いたいのですが、Cell.swiftでは使うことはできないのでしょうか。
何問目のどのボタンが押されたかをセーブしたいです。
各ソースコード
QuizCell
Swift
import UIKit import Foundation protocol QuizCellDelegate { func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int) } class QuizCell: UITableViewCell { var currentQuizButtonIndex: Int = 0 { didSet { let value = self.currentQuizButtonIndex self.updateCurrentQuizButton(value) if let delegate = self.delegate { delegate.quizCellDidChangeCurrentButtonIndex(self, index: value) } } } @IBOutlet weak var questionLabel: UILabel! @IBOutlet var answerButtons: [UIButton]! var delegate: QuizCellDelegate? override func awakeFromNib() { super.awakeFromNib() //print("ここまできてるか確認") // Initialization code } @IBAction func didTapQuizButton(_ sender: UIButton) { if let index = self.answerButtons.firstIndex(of: sender){ self.currentQuizButtonIndex = index delegate?.quizCellDidChangeCurrentButtonIndex(self, index: index) print(index) } } private func updateCurrentQuizButton(_ currentIndex: Int){ for (index, answerButton) in self.answerButtons.enumerated(){ if index == currentIndex { answerButton.setTitle("●", for: .normal) } else { answerButton.setTitle("○", for: .normal) } } } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
ViewControllerコード
Swift
import UIKit class AnswerQuizViewController: UIViewController, UITableViewDelegate { var q: QuestionSeries! @IBOutlet weak var quizTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() quizTableView.dataSource = self quizTableView.delegate = self // cell xibファイルを使うときは書く必要があるやつ。 // quizTableView.register(UINib(nibName: K.Cells.QuizCellNibName, bundle: nil), forCellReuseIdentifier: K.Cells.QuizCellIdentifier) quizTableView.register(UINib(nibName: "QuizCell", bundle: nil), forCellReuseIdentifier: "QuizCellIdentifier") // Do any additional setup after loading the view. } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation // override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // // Get the new view controller using segue.destination. // // Pass the selected object to the new view controller. //// if segue.identifier == K.Segue.checkResult { //// let resultViewController = segue.destination as! ResultViewController //// answerQuizViewController.q = //// print(answerQuizViewController.q) // } } // MARK: - quizTableViewのアレンジ extension AnswerQuizViewController: UITableViewDataSource, QuizCellDelegate { func quizCellDidChangeCurrentButtonIndex(_ cell: QuizCell, index: Int) { if let indexPath = self.quizTableView.indexPath(for: cell){ self.q.question[indexPath.row].answer = index print(index) }else{ print("ここきてます") } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return q.question.count //print(q.question.count) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let question = q.question[indexPath.row] let cell = quizTableView.dequeueReusableCell(withIdentifier: K.Cells.QuizCellIdentifier, for: indexPath) as! QuizCell cell.questionLabel.text = question.text // print(question.text) return cell } }
また、もし他の方法で実装できるなどご助言あれば、ぜひご教授いただけますと幸いです。
よろしくお願いいたします。
まだ回答がついていません
会員登録して回答してみよう