如何将表格视图单元格中 UI 按钮的值输入到 Table 视图控制器中声明的变量?

How to input the value from UI button in Teble View cell to the variable declared in Table View Controller?

我想将填充按钮(●)的值保存到数组“q.answer[indexPath.row]”关于每个问题“q.question[indexPath.row] ). currentQuizButtonIndex 当前每次当◯ 变为● 时通过点击更新。但是,我不知道如何保存到 TableViewController 中声明的变量 q 中。 View Controller display

关于 QuizCell.swift 的代码(TableCell 大约有 5 个按钮和 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
}

}

关于视图控制器的代码

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

如果您有任何关于通过其他方式实现它的想法,这也会很有帮助。 谢谢。

创建一个静态数组并将数据存储到该数组中如何? 点击按钮后,您可以将其附加到该静态数组中。

创建一个新文件。只是一个基本的“Swift 文件”。

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

然后通过以下方式追加数据:

structName.q.append()

终于得到你的数据槽:

structName.q[index]