How to implement multiple choice questions in table view OR how to implement radio buttons

How to implement multiple choice questions in table view OR how to implement radio buttons

How to implement multiple choice questions in table view OR how to implement radio buttons

Are you with a sensitive skin type?

Whether you have experienced pricking-pain desquamation, itching, or dry skin sensation during seasonal alternate.

Here we are going to explain about implementing multiple choice selections in a table view using radio buttons. Now let’s start by taking a table view inside a ViewController.

    
    @IBOutlet weak var tableView: UITableView!
    let numberOfCells = 10
    var models = [Model]()
    

in viewDidLoad:-

    
        0..<numberOfCells).forEach { _ in
            self.models.append(Model())
    

Now create a model class like this example:-

    
        import Foundation
        import UIKit

        class Model {
            var isOptionA_Selected = false
            var isOptionB_Selected = false
            var isOptionC_Selected = false
            var isOptionD_Selected = false
        }
    

Now let’s write code in table view cell class, create a variable like given below:-

    
        var buttons: [UIButton]!
        var buttonTapHandler: (()->())?
        var model: Model?
    

Now add actions of all button to the same action class like given below attached all button action to the same IBAction:-

    
        @IBAction func buttonTappedHandle(_ sender: Any) {
            model?.isOptionA_Selected = (sender as? NSObject == buttonA)
            model?.isOptionB_Selected = (sender as? NSObject == buttonB)
            model?.isOptionC_Selected = (sender as? NSObject == buttonC)
            model?.isOptionD_Selected = (sender as? NSObject == buttonD)
            self.updateUI()    
        }
    

Now create a custom function for changing their button background colour like given below:-

    
        func updateUI() {
            buttonA.backgroundColor = (model?.isOptionA_Selected ?? false) ? .green : .gray
            buttonB.backgroundColor = (model?.isOptionB_Selected ?? false) ? .green : .gray
            buttonC.backgroundColor = (model?.isOptionC_Selected ?? false) ? .green : .gray
            buttonD.backgroundColor = (model?.isOptionD_Selected ?? false) ? .green : .gray
        }
    

Now again go into our main view controller:-

In table view delegate method

cellForRowIndex
, write something like this:-

    
        cell.configure(with: models[indexPath.row])
            cell.buttonTapHandler = {
                print(indexPath.row)

    

So that’s it, We have implemented multiple choice questions OR radio buttons in Table View.

I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.

Our Offices

INDIA

F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163

USA

13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079

CANADA

15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147