2

I just got some problem regarding to my iOS project. I'm using xcode 7.0.1 right now. The problem is I can't get UISwitch value from each UITableViewCell. There are 4 UITableViewCell. So I already done created an object to store all UISwitch values.

My model

import Foundation

let instanceQuiz = Quiz()
let instanceSwitchModel = SwitchModel()

class Quiz: NSObject {
    var answerList: [Int] = []
}

class SwitchModel : NSObject {
var switch1 : Bool? = false
var switch2 : Bool? = false
var switch3 : Bool? = false
var switch4 : Bool? = false
}

My controller

import UIKit
import SwiftyJSON

class QuizQuestionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var question: UILabel!
    @IBOutlet weak var tableView: UITableView!
    var quizs:[JSON]! = ["a","b","c","d"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return quizs.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("QuizCell", forIndexPath: indexPath)

        let quiz = quizs[indexPath.row]
        let switchView: UISwitch = UISwitch(frame: CGRectZero)

        cell.accessoryView = switchView
        if let nameLabel = cell.viewWithTag(100) as? UILabel {
            nameLabel.text = quiz["mcq_answer"].stringValue
        }

        return cell
    }

@IBAction func btnSubmit(sender: AnyObject) {
        /* how to capture all UISwitch values and store it into model? */
    }

}

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • " I already done created an object to store all UISwitch values". What is the problem? – tuledev Nov 04 '15 at 06:54
  • i want to retrieve all UISwitch values from UITableViewCell after user click submit button. do you know how? i cant create IBOutlet for UISwitch because it's repetitive content. what I saw is I need to do it programmatically. – Nurdin Nov 04 '15 at 06:56
  • I think " I already done created an object to store all UISwitch values" is a solution. Create an array store their value. E.g: [0,0,...]. When user turn on/off one ->update array.E.g: [1,0,...]. – tuledev Nov 04 '15 at 07:00

2 Answers2

1

You can create a view model for switches, which you can populate with initial values when user move to the screen and whenever user changes state of any switch update the respective value of switch in view model. Just make sure your view model and your switches have same value, and always read values from view model instead of directly from UISwitch in tableviewcell.

class SwitchModel : NSObject {
    var switch1 : Bool? = false
    var switch2 : Bool? = false
    var switch3 : Bool? = false
    var switch4 : Bool? = false
}

Declare in QuizQuestionViewController

var switchModel = SwitchModel() 

In viewDidLoad you can populate initial values for switches.

Implement delegate from your customTableView and have values of switchModel update when user move the switches visually

TestProject might be helpful

Usama
  • 1,945
  • 1
  • 13
  • 20
  • btw, how to know which UISwitch I select using func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { }? – Nurdin Nov 04 '15 at 07:34
  • you will not us didSelectRowATIndexPath, create a custom cell with implementation for UISwitch value changed, and fire a delegate every time value of switch for tableviewcell changes – Usama Nov 04 '15 at 07:38
  • i almost there. still figure out how to create a USwitch listener attach to UITableViewCell. – Nurdin Nov 04 '15 at 07:42
  • have you created custom UITableViewCell class? – Usama Nov 04 '15 at 07:45
  • No, i directly create UISwitch into accessoryView. you can check my edited question. – Nurdin Nov 04 '15 at 07:46
  • 1
    let me attach a test project for better understanding – Usama Nov 04 '15 at 08:59
  • test project added in the answer, this might be helpful for you – Usama Nov 04 '15 at 09:24
0

i would suggest you to use one more variable in your question model, for example: questionText and switchValue (Boolean) // you can give the switchValue a default value or initialize it when you initialize your question.

In your viewcontroller, when you initialize your Question with json data, set the questionText. Save the question in an array of Questions.

Populate the uitableview with the array values. Check in every cell the switchValue and turn the switch on/off depanding on this value.

In your DidSelectRowAtIndexpath, set the switchValue to true or false if the user taps on a cell. Thats it.

Hope this helpfull.

dan
  • 873
  • 8
  • 11