简体   繁体   中英

number of radio buttons in a static tableview cell in swift

I have been trying to add 3 radio buttons for gender selection in a static tableviewcell.But am not able to do that.Can any one help to do this.

func setGenderCell(indexPath : IndexPath) -> UITableViewCell {

    let cell  :  SetGenderTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "SetGenderTableViewCell", for: indexPath) as! SetGenderTableViewCell
    cell.subject.text = self.profileData[indexPath.row].getName()
    cell.genderImage.image = UIImage(named: self.profileData[indexPath.row].getImage())
    cell.maleButton.addTarget(self, action: #selector(self.maleGenderSelect), for: .touchUpInside)
    cell.femaleButton.addTarget(self, action: #selector(self.femaleGenderSelect), for: .touchUpInside)
    cell.othersButton.addTarget(self, action: #selector(self.othersGenderSelect), for: .touchUpInside)
    cell.maleButton.tag = 1
    cell.femaleButton.tag = 2
    cell.othersButton.tag = 3

    return cell
}

@objc func maleGenderSelect(){
    let cell  :  SetGenderTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "SetGenderTableViewCell", for: IndexPath) as! SetGenderTableViewCell

    cell.maleRadioImage.image = UIImage(named: "")
    cell.femaleRadioImage.image = UIImage(named: "")
    cell.othersRadioImage.image = UIImage(named: "")

}

@objc func femaleGenderSelect(){

}

@objc func othersGenderSelect(){

}

Reference Image:

在此输入图像描述

Refer this pseudo Code,


Create GenderCellDelegate

protocol GenderCellDelegate : class {
    func genderSelected(_ gender : Gender)
}

Enum For gender

enum Gender : Int {
    case male = 1, female, other
}

GenderTableViewCell

class GenderTableViewCell: UITableViewCell {

    @IBOutlet weak var btnMale : UIButton!
    @IBOutlet weak var btnFemale : UIButton!
    @IBOutlet weak var btnOther : UIButton!

    weak var delegate : GenderCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        // Your can do below from XIB also
        // Set buttons images for selcted and normal state
        btnMale.setImage(UIImage(named: "radio-on"), for: .selected)
        btnFemale.setImage(UIImage(named: "radio-on"), for: .selected)
        btnOther.setImage(UIImage(named: "radio-on"), for: .selected)
        btnMale.setImage(UIImage(named: "radio-off"), for: .normal)
        btnFemale.setImage(UIImage(named: "radio-off"), for: .normal)
        btnOther.setImage(UIImage(named: "radio-off"), for: .normal)

        // Optional - if required
        btnMale.isSelected = true // For default selection

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    @IBAction func btnMaleSelected(_ sender : UIButton) {
        self.btnMale.isSelected = true
        self.btnFemale.isSelected = false
        self.btnOther.isSelected = false
        delegate?.genderSelected(.male)
    }

    @IBAction func btnFemaleSelected(_ sender : UIButton) {
        self.btnMale.isSelected = false
        self.btnFemale.isSelected = true
        self.btnOther.isSelected = false
        delegate?.genderSelected(.female)
    }

    @IBAction func btnOtherSelected(_ sender : UIButton) {
        self.btnMale.isSelected = false
        self.btnFemale.isSelected = false
        self.btnOther.isSelected = true
        delegate?.genderSelected(.other)
    }


}

Your Controller

class MyController : UIViewController , UITableViewDelegate, UITableViewDataSource, GenderCellDelegate {

     :
     :


    func genderSelected(_ gender: Gender) {

        switch gender {
        case .male:
            print("Male selected")
            break
        case .female:
            print("Female selected")
            break
        case .other:
            print("Other selected")
            break
        }

        // reload tableview row for gender cell
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let genderCell = tableView.dequeueReusableCell(withIdentifier: "GenderTableViewCell") as! GenderTableViewCell

        genderCell.delegate = self

        return genderCell
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM