Home > other >  Saving Tableview Button tapped in total in Swift
Saving Tableview Button tapped in total in Swift

Time:07-21

I want the code to count yes and no, then pass the total number of yeses and noes pressed to the view controller when you have gone through all the questions. What is the best way to do this? When I try it, each cell counts up independently and not all together. Thanks for the help

The Custom Cell:

import UIKit

class TableViewCellQuestion_3: UITableViewCell {

    @IBOutlet weak var button_nein: UIButton!
    @IBOutlet weak var button_Ja: UIButton!
    @IBOutlet weak var unterFragenLabel: UILabel!
    var x = 0
    var y = 0
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func button_Ja_Tapped(_ sender: Any) {
        x  = 1
        print(x)
    }
    
    
    @IBAction func button_Nein_Tapped(_ sender: Any) {
        y  = 1
        print(y)
    }
}

The View Controller:

import UIKit

class TableViewController3: UIViewController {

    
    @IBOutlet weak var antwortLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var nextQuestion: UIButton!
    
    
    
    var questions = [Questions]()
    var questionCount = 0
    var x = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        parseJson()
        tableView.rowHeight = 100
        // Do any additional setup after loading the view.
        
    }
    

    func loadData() {
        // code to load data from network, and refresh the interface
        tableView.reloadData()
    }
    
    private func parseJson(){
        
            guard let path = Bundle.main.path(forResource: "Fragen_Funktional", ofType: "json") else {
                return
            }
            let url = URL(fileURLWithPath: path)
            
           
            
            do {
                let jsonData = try Data(contentsOf: url)
                questions = try JSONDecoder().decode([Questions].self, from: jsonData)
                return
            }
            catch {
                print("Error: \(error)")
            }
        }
    
    @IBAction func nextQuestionBtn_Tapped(_ sender: UIButton) {
       
        
        let length = questions.count
        print(length)
        
        if questionCount == questions.count - 1 {
           print("stop")
            nextQuestion.setTitle("Formular Beenden", for: .normal)
            let controller = storyboard?.instantiateViewController(withIdentifier: "endscreen") as! UINavigationController
            controller.modalPresentationStyle = .fullScreen
            present(controller, animated: true, completion: nil)
        } else {
            questionCount  = 1
             // Load the data
             self.loadData()
        }
    }
}

extension TableViewController3 : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return questions[questionCount].questionpoints.count
        
        
       
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let text = questions[questionCount].question
        let cell = tableView.dequeueReusableCell(withIdentifier: "Fragen3", for: indexPath as IndexPath) as! TableViewCellQuestion_3
        
        cell.unterFragenLabel.text =  questions[questionCount].questionpoints[indexPath.row].description
       
        
        
        
        
        antwortLabel.text = text
        
        return cell
    }
}


CodePudding user response:

Set up your table view cells to have a delegate. Define a protocol for the delegate that lets your cells notify the delegate when the user taps the Ja or Nein buttons. (Let's call it TableViewCellQuestion3Protocol. Set up your IBActions to send a message to the delegate.

Have you view controller conform to your protocol. In your cellForRowAt() method, after dequeuing a cell, set its delegate property to self.

(You might need to read up on the delegate pattern in order to understand what I'm suggesting. )

  • Related