2017-07-29 20 views
0

私は、選択されたときにイメージを変更するtableViewを持っています。これはすべて正常に動作しますが、tableCellを選択すると画像が変更されますが、スクロールすると、選択しなかった別のセルの画像も変更されます。Checkbox on tableview duplicating、Swift、iOS

以下は私のコードです。

did select関数でdetqueureusableセルを使用すると、選択したときに画像が変更されません。

+0

これはあなたのquestion-お答えしますします。https://stackoverflow.com/questions/43686354/filtertableviewcontroller-reloadrows-reloading-rows-only-on-first-callを。実際には、チェックボックスの状態を保存する必要があります。 –

+0

ここに私のデモhttps://www.dropbox.com/s/e43zk55surlwjlk/CollectionCkeck.zip?dl=0コレクションビューがありますが、ロジックはあなたを助けるかもしれません –

答えて

1

tableView.dequeueReusableCell(_)を使用できます。問題は、選択したセルのステータスを維持していないことです。

例:

class viewController: UIVieWController, UITableViewDelegate, UITableViewDataSource { 

    var selectedCellList = [IndexPath]() 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturesCell") as! FeaturesCell 

     cell.featuresLabel.text = self.items[indexPath.row] 

     if let _ = selectedCellList.index(of: indexPath) { 
      // Cell selected, update check box image with tick mark 
      cell.checkImage.image = #imageLiteral(resourceName: "tick-inside-circle") 
     } else { 
      // Cell note selected, update check box image without tick mark 
      cell.checkImage.image = #imageLiteral(resourceName: "No-tick-inside-circle") 
     } 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     pickedFeatures.append(items[indexPath.row]) 

     if let index = selectedCellList.index(of: indexPath) { 
      selectedCellList.remove(at: index) 
     } else { 
      selectedCellList.append(indexPath) 
     } 
     tableView .reloadRows(at: [indexPath], with: .automatic) 
    } 

} 
+0

ありがとう@Subramanian。これは完璧に機能しましたが、あなたの説明も助けになりました。場合によっては、他の人がより優れたプログラマーになるための説明もあります。それではありがとうございます。ではごきげんよう。 –

関連する問題