2016-03-29 13 views
1

私は再利用可能なセルを持つテーブルビューを持っています。行が選択されているかどうかを示すチェックボックスとしてイメージビューを配置しました。行が選択されると、画像はそれに応じて正しく変化するが、再利用されている別のセルの画像も変化する。私はモデルを状態に保存し、それを反映させることによって試しましたが、indexPathは特定のセルの後で繰り返されるため、問題に取り組むことができません。セルを選択するときのチェックボックス機能iOSスウィフト

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 
    } 

答えて

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 
      } 
+0

ええ、それは感謝しました。 –

+0

あなたのウェルカム! –

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 
} 
+0

ありがとうございました。 –

関連する問題