Home > database >  get selected button from all the cells
get selected button from all the cells

Time:01-13

I am new to swift and iOS. I am stuck at one point of my development. I hope any one of you. The problem is I have a table view cell, in which I am having one question and two buttons with yes or no title. Initially both buttons will not be selected(white background). After click on button it should be selected(blue background). Any one of the button should be selected at the time. I have 5 of these cells totally.

At footer of tableview I have continue button. After answering all these questions the button will be enabled. Here, I need to check how many yes and no available.

I have implemented the UI and everything. But at continue button action I couldn't get the number of yes es and nos. I tried but not able to figure out. Could anyone help me please.

CodePudding user response:

I am assuming that you are using Model to display the cells in the tableView For example:

struct Question {
    var text: String?
    var selectedAnswer:Bool? // make it true if user selected yes or make it false if user selected no
    
    func getAllAnswers(questions:[Question])->[Bool] {
        return questions.compactMap{ $0.selectedAnswer }
    }
}

By using getAllAnswers() you will get an array and get the count from that

  • Related