0

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!

EMouse
  • 1
  • 1
  • You have to cast the cell returned by `visibleCells` to your subclass using `as? [MyCellClass] ?? []`, however it is a mistake to try and access data from cells. Cells are reused and some cells may not be onscreen (hence `visibleCells`). As the user interacts with the cells (turns checkboxes on and off) you need to update the data model. You can use delegation or a closure. E.g. https://stackoverflow.com/questions/28659845/how-to-get-the-indexpath-row-when-an-element-is-activated/38941510#38941510 – Paulw11 Jul 22 '22 at 21:48
  • @Paulw11 using delegation worked perfectly, thank you so much! And thank you for teaching me about `visibleCells` too – EMouse Jul 23 '22 at 15:51

0 Answers0