繁体   English   中英

选择单元格iOS swift时的复选框功能

[英]check box feature when selecting a cell iOS swift

我有一个带有可重用单元格的表格视图。 我已将图像视图放置为一个复选框,用于指示该行是否被选中。 Whenever a row is selected then the image changes accordingly correctly but another cell's image which are being reused also change. 我尝试通过将状态保存到模型并将其反映到视图中仍然无法解决问题,因为 indexPath 在某些单元格之后继续重复。

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
    }

加载表时分配未选中的图像。 这是slotion

     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
            }

在此处输入图像描述原因是您未选中时未重置图像。 这是一个简单的修复:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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