Here is the process to add a radio button with a check mark on a TableView cell using Swift:
class RadioTableViewCell: UITableViewCell {
@IBOutlet weak var radioButton: UIButton!
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Setup the radio button
radioButton.setImage(UIImage(named: "unchecked"), for: .normal)
radioButton.setImage(UIImage(named: "checked"), for: .selected)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RadioCell", for: indexPath) as! RadioTableViewCell
// Set the label text for the cell
cell.label.text = "Option \(indexPath.row + 1)"
return cell
}
didSelectRowAt
delegate method, toggle the selected state of the radio button and deselect the cell.override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! RadioTableViewCell
// Toggle the radio button's selected state
cell.radioButton.isSelected = !cell.radioButton.isSelected
// Deselect the cell
tableView.deselectRow(at: indexPath, animated: true)
}
isSelected
property of the radio button to determine which option was chosen.Asked: 2023-01-09 11:00:00 +0000
Seen: 14 times
Last updated: Dec 05 '21