title: How to input the value from UI button in Teble View cell to the variable declared in Table View Controller?
body: <p>I want to save the value of filled button(●) into the array &quot;q.answer[indexPath.row]&quot; about each question &quot;q.question[indexPath.row]).
  currentQuizButtonIndex is currently renewed every time when ◯ changes to ● by tapping. However, I have no idea how to save in to variable q which is declared in TableViewController.
  <a href="https://i.stack.imgur.com/GDabO.png" rel="nofollow noreferrer">View Controller display</a></p>
  <p><strong>Code about QuizCell.swift (TableCell which is about 5 buttons and UIlabel.)</strong></p>
  <pre><code>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(&quot;ここまできてるか確認&quot;)
      // 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(&quot;●&quot;, for: .normal)
          } else {
              answerButton.setTitle(&quot;○&quot;, for: .normal)
  
          }
      }
  }
  
  
  override func setSelected(_ selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)
  
      // Configure the view for the selected state
  }
  
  }
  </code></pre>
  <p><strong>Code about View Controller</strong></p>
  <pre><code>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: &quot;QuizCell&quot;, bundle: nil), forCellReuseIdentifier: &quot;QuizCellIdentifier&quot;)
  
      // 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(&quot;ここきてます&quot;)
          }
      }
  
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return q.question.count
      //print(q.question.count)
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; 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
  }
  }
  </code></pre>
  <p>It is also helpful if you have any idea of implementing this by alternative way.
  Thanks.</p>