Home > Mobile >  How to input the value from UI button in Teble View cell to the variable declared in Table View Cont
How to input the value from UI button in Teble View cell to the variable declared in Table View Cont

Time:12-09

I want to save the value of filled button(●) into the array "q.answer[indexPath.row]" about each question "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. View Controller display

Code about QuizCell.swift (TableCell which is about 5 buttons and UIlabel.)

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
}

}

Code about View Controller

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
}
}

It is also helpful if you have any idea of implementing this by alternative way. Thanks.

CodePudding user response:

How about you create a static array and store your data into that array. when the button is tapped you can append it into that static array.

Create a new file. Just a basic "Swift file".

struct structName {
    static var qArray: [String] = []
}

Then append data by:

structName.q.append()

Finally get your data trough:

structName.q[index]
  • Related