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? */
}
}