首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选中单元格iOS cell时的复选框功能

选中单元格iOS cell时的复选框功能
EN

Stack Overflow用户
提问于 2016-03-29 15:19:05
回答 2查看 749关注 0票数 1

我有一个带有可重用单元格的表视图。我已将图像视图放置为复选框,以指示是否选中了行。每当选定一行时,图像就会相应地发生相应的变化,但是正在被重用的另一个单元格的图像也会发生变化。我试图通过保存状态来建模,并将其反映到视图中,但我仍然无法解决这个问题,因为indexPath在某些单元格之后继续重复。

代码语言:javascript
复制
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
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-29 16:03:42

加载表时分配未检查的图像。这是懒散的地方

代码语言:javascript
复制
     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
            }
票数 1
EN

Stack Overflow用户

发布于 2016-03-29 15:33:23

在这里输入图像描述原因是,当图像未被选中时,没有重置它。这是一个简单的解决方案:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36288423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档