チェック状態をデータで持つ必要があります、簡単に記述すると以下の様な感じになると思います。
※ 自分作っているデータ配列のクラスにチェック状態のプロパティを追加してみてください。
swift
1import UIKit
2
3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
4
5 @IBOutlet weak var tableView: UITableView!
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9
10 tableView.estimatedRowHeight = 44
11 tableView.rowHeight = UITableViewAutomaticDimension
12 }
13
14 // Data Array
15 var checkDataArray = [true, true, true]
16
17 // MARK: - TableView Delegate & DataSource
18
19 // Row Count
20 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
21 return checkDataArray.count
22 }
23
24 // Generate Cell
25 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
26 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
27
28 // セルにチェック状態を反映
29 if checkDataArray[indexPath.row] {
30 cell.accessoryType = .checkmark
31 } else {
32 cell.accessoryType = .none
33 }
34
35 return cell
36 }
37
38 // Select Cell
39 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
40 tableView.deselectRow(at: indexPath as IndexPath, animated: true)
41
42 // チェック状態を反転してリロードする
43 checkDataArray[indexPath.row] = !checkDataArray[indexPath.row]
44 tableView.reloadData()
45 }
46}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。