我有一个包含可重用单元格的表格 View 。我将 ImageView 放置为一个复选框,用于指示该行是否被选中。 Whenever a row is selected then the image changes accordingly correctly but another cell's image which are being reused also change.我已经尝试通过将状态保存到模型并将其反射(reflect)到 View 中,但我仍然无法解决这个问题,因为 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
}
最佳答案
加载表格时分配未经检查的图像。 这是 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
}
关于ios - 选择单元格 iOS swift 时的复选框功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288423/