我有一个带有可重用单元格的表视图。我已将图像视图放置为复选框,以指示是否选中了行。每当选定一行时,图像就会相应地发生相应的变化,但是正在被重用的另一个单元格的图像也会发生变化。我试图通过保存状态来建模,并将其反映到视图中,但我仍然无法解决这个问题,因为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
}发布于 2016-03-29 16:03:42
加载表时分配未检查的图像。这是懒散的地方
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
}发布于 2016-03-29 15:33:23
在这里输入图像描述原因是,当图像未被选中时,没有重置它。这是一个简单的解决方案:
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
}https://stackoverflow.com/questions/36288423
复制相似问题