laxman khanal
laxman khanal

Reputation: 998

check box feature when selecting a cell iOS swift

I have a table view with reusable cells. I have placed image view as a check box for indicating either the row is selected or not. Whenever a row is selected then the image changes accordingly correctly but another cell's image which are being reused also change. I have tried by saving state to model and reflecting it to view still I am unable to tackle the problem since indexPath goes on repeating after certain cells.

var allvacc: [[String:String]] = [
    [
        "id":"0",
        "name":"BCG",
        "timeAfterBirth":"1",
        "description":"BCG stands for Bacillus Calmette–Guérin given to a baby 1 times as soon as possible after birth for protection against Tuberculosis",
        "isChecked": "false",
    ],
    [
        "id":"1",
        "name":"DPT-HepB-HiB - I",
        "timeAfterBirth":"42",
        "description":"DPT refers to a class of combination vaccines against three infectious diseases in humans: diphtheria, pertussis (whooping cough), and tetanus. Hepatitis B adn Hemophilius Influenza. This vaccine is taken 3 times in 6, 10 and 14 weeks.",
        "isChecked": "false",
    ] 
]

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        allVacc[indexPath.row]["isChecked"] = "true"
        vaccineTableView.reloadData()


    }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        guard let checkListCell = tableView.dequeueReusableCellWithIdentifier("vaccineCheckListCell") as? VaccineCheckListCell else {
            return UITableViewCell() }
        checkListCell.vaccineNameLabel.text = vaccinationList[indexPath.row].name
        if  allVacc[indexPath.row]["isChecked"] == "true" {
            checkListCell.vaccineStatusImage.image = UIImage(named: "ic_check_circle_yellow_48dp")
        }
        return checkListCell
    }

Upvotes: 1

Views: 771

Answers (2)

Surely
Surely

Reputation: 1709

enter image description hereThe reason is you did not reset the image when it is not checked. This is a simple fix:

if  allVacc[indexPath.row]["isChecked"] == "true" {
    checkListCell.vaccineStatusImage.image = UIImage(named: "ic_check_circle_yellow_48dp")
} else {
    checkListCell.vaccineStatusImage.image = nil //or your unchecked image
}

Upvotes: 0

vihar sheladia
vihar sheladia

Reputation: 54

assign unchecked image when table is loaded. here is the sloution

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                guard let checkListCell = tableView.dequeueReusableCellWithIdentifier("vaccineCheckListCell") as? VaccineCheckListCell else {
                    return UITableViewCell() }
                checkListCell.vaccineNameLabel.text = vaccinationList[indexPath.row].name
                checkListCell.vaccineStatusImage.image = your unchecked image

 if  allVacc[indexPath.row]["isChecked"] == "true" {
                    checkListCell.vaccineStatusImage.image = UIImage(named: "ic_check_circle_yellow_48dp")
                }
                return checkListCell
            }

Upvotes: 1

Related Questions