I am entirely new at Swift, and am building a referral form. Some pages of the form have tickboxes where the user can select what type of support they need. I've created these tickboxes as cells for a UITableView. They have their own view and nib, and are made up of a button and an image for feedback. Here's the code:
class TickboxCell: UITableViewCell {
@IBOutlet weak var supportBox: UIView!
@IBOutlet weak var supportBtn: UIButton!
@IBOutlet weak var checkmark: UIImageView!
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 buttonPressed(_ sender: UIButton) {
if supportBtn.isSelected == false {
supportBtn.isSelected = true
checkmark.image = UIImage(named: K.Images.checked)
} else {
supportBtn.isSelected = false
checkmark.image = UIImage(named: K.Images.notChecked)
}
}
}
In the ViewController for the page, I'm trying to create a function whereby, when the user presses the Next button to go to the next page of the form, all the labels of the buttons that have been selected are stored and then passed onto the final page of the referral form, ready to be emailed.
This is where I'm at with the function:
func getTicks() {
var ticks: [String:String?] = [:]
for cell in self.tableView.visibleCells {
if cell.supportBtn.isSelected == true {
if let title = cell.supportBtn.titleLabel?.text {
ticks.updateValue("Yes", forKey: title)
}
}
}
}
Of course, this doesn't work because the visible cells don't have what I'm looking for. They only have properties like baseClass and frame.
In my function for the tableView, there's the line
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath) as! TickboxCell
I know that's where it makes the tableView cells into the Tickbox cells - I set the titleLabels for the cell buttons after that. So can I work this into the above function somehow? Or is there another way to tap into the selected status and titles of my buttons?
This is my first ever question on here, apologies if there's anything wrong with it!