我有一个带有可重复使用单元格的表视图。我已将图像视图作为复选框放置,以指示是否选择了行。每当选择一行时,图像会相应地正确改变,但正在重复使用的另一个细胞图像也会改变。我已经尝试将状态保存到模型并反映它仍然无法解决问题,因为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
}
答案 0 :(得分:1)
在加载表时分配未经检查的图像。 这是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
}
答案 1 :(得分:0)
enter image description here原因是未检查图像时未重置图像。这是一个简单的修复:
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
}