いつもお世話になります。
常時並び替え可能なテーブルビューにチェックマークを設定したところ、並び替え処理後に、チェックマークが維持できなくて困っています。
TableViewリロード時に、チェックマークが維持できなくて困っています。
UserDefaultなどをつかった、チェック済セルの永続化に必要なので、
tableView.allowsMultipleSelectionDuringEditing = true に設定した際にチェックマークの状態を示すセルのプロパティ名をご存知でしたら教えてください。
以前教えていただいたUILabelを使用して作ったものを、このチェックマークに置き換えたいと思うのですが、そもそも、このチェックマークの使い道自体が間違っているのでしょうか?
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 var typesOfCatsArray: [String] = ["ジバニャン", "ワルニャン", "フユニャン", "イヌニャン", "サルニャン", "イチゴニャン"] 6 // 選択済みの猫の種類を記憶する配列 7 var selectedCatsArray: [String] = [] 8 9 @IBOutlet weak var typeOfCatsTableView: UITableView! 10 11 12 //MARK: - LifeCycle - 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 // tableViewの編集を可能にする 17 typeOfCatsTableView.isEditing = true 18 //typeOfCatsTableView.allowsSelectionDuringEditing = true 19 typeOfCatsTableView.allowsMultipleSelectionDuringEditing = true 20 } 21 22 override func didReceiveMemoryWarning() { 23 super.didReceiveMemoryWarning() 24 } 25 26 //MARK: - UITableViewDataSource - 27 28 // numberOfCell 29 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 30 return typesOfCatsArray.count 31 } 32 33 // generateCell 34 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 35 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 36 37 let typeOfCatStr = typesOfCatsArray[indexPath.row] 38 39 // typeOfCatsTableView.reloadData()時に、ここで選択済み配列に入っている猫の種類Cellにチェックマークを付けたいが記述方法がわからない。 40 if selectedCatsArray.contains(typeOfCatStr) == true { 41 //cell.isSelected = true 42 } else { 43 //cell.isSelected = false 44 } 45 46 cell.textLabel!.text = typeOfCatStr 47 48 return cell 49 } 50 51 52 //MARK: - UITableViewDelegate - 53 54 // selectCell 55 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 56 57 let selectedCatStr = typesOfCatsArray[indexPath.row] 58 if selectedCatsArray.contains(selectedCatStr) == false { 59 // 選択済み猫の種類を保存する 60 selectedCatsArray.append(selectedCatStr) 61 62 // 他のTableViewの配列に値を格納する 63 64 } 65 } 66 67 // deselectCell 68 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 69 70 let deselectedCatStr = typesOfCatsArray[indexPath.row] 71 if let deleteCatIndex = selectedCatsArray.index(of: deselectedCatStr) { 72 // 選択済み猫の種類を解除する 73 selectedCatsArray.remove(at: deleteCatIndex) 74 75 // 他のTableViewの配列から値を削除する 76 } 77 } 78 79 // sortable 80 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 81 return true 82 } 83 84 // editArray 85 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 86 let targetCatStr = typesOfCatsArray[sourceIndexPath.row] 87 typesOfCatsArray.remove(at: sourceIndexPath.row) 88 typesOfCatsArray.insert(targetCatStr, at: destinationIndexPath.row) 89 90 // リロードを呼ばなければチェックマークが維持される。 91 typeOfCatsTableView.reloadData() 92 } 93 94} 95 96
↓UILabelを使用して作ったもの。
swift
1 2//簡素化したコード。これ以外は、StoryboardにTableView・Cellを配置してCellのidentifierを設定、TableViewをOutlet接続しただけです。 3 4import UIKit 5 6class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 7 8 var typesOfCatsArray: [String] = ["ジバニャン", "ワルニャン", "フユニャン", "イヌニャン", "サルニャン", "イチゴニャン"] 9 10 @IBOutlet weak var typeOfCatsTableView: UITableView! 11 12 13 //MARK: - LifeCycle - 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // tableViewの編集を可能にする 18 typeOfCatsTableView.isEditing = true 19 //typeOfCatsTableView.allowsSelectionDuringEditing = true 20 typeOfCatsTableView.allowsMultipleSelectionDuringEditing = true 21 } 22 23 override func didReceiveMemoryWarning() { 24 super.didReceiveMemoryWarning() 25 } 26 27 //MARK: - UITableViewDataSource - 28 29 // numberOfCell 30 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 31 return typesOfCatsArray.count 32 } 33 34 // generateCell 35 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 36 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 37 38 cell.textLabel!.text = typesOfCatsArray[indexPath.row] 39 40 return cell 41 } 42 43 // sortable 44 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 45 return true 46 } 47 48 // editArray 49 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 50 let targetCatStr = typesOfCatsArray[sourceIndexPath.row] 51 typesOfCatsArray.remove(at: sourceIndexPath.row) 52 typesOfCatsArray.insert(targetCatStr, at: destinationIndexPath.row) 53 typeOfCatsTableView.reloadData() 54 } 55}
解決方法備忘録
swift
1// generateCell 2 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 3 let cell = typeOfCatsTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 4 5 let typeOfCatStr = typesOfCatsArray[indexPath.row] 6 7 // typeOfCatsTableView.reloadData()時に、ここで選択済み配列に入っている猫の種類をselectする 8 print("selectedCatsArray:",selectedCatsArray) 9 if selectedCatsArray.contains(typeOfCatStr) == true { 10 typeOfCatsTableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) 11 } else { 12 typeOfCatsTableView.deselectRow(at: indexPath, animated: false) 13 } 14 15 cell.textLabel!.text = typeOfCatStr 16 17 return cell 18 } 19 20コード
質問のコードではチェックマークの表示/非表示のコードが無いですよね?でもgifにはチェックマークが付いているし?何を質問しているのでしょうか?
返信ありがとうございます。そうなんです。チェックマークの表示/非表示のコードを記述しなくても、チェック機能が動いているので、どの部分がチェック機能を担っているのかわからなくて困っています。
通常、didselect内でセルに行った変更をcellForRowAt内でリロード時に適用して上げればよいのかと思いますが…。この場合、didselect内で何も処理をしなくてもチェックマークがついてしまうので…。
いや、押したときの表示/非表示の処理ではなく、チェックマークそのものの表示/非表示のコードのことを言ってます。
お手数おかけします。チェックマークそのものの表示/非表示をどうやっているのかがわからないのです。viewDidLoad内で、tableViewのプロパティを2つtrueにしてあげたところ、チェックボックスがセルの左側に表示さえれて、タップするだけで、チェックマークがつくようになったので…。どうやってチェックを付けているかがわからないのです。チェックをつけるコードがわかれば、チェックを自分で操作できると思うのですが...。
えと、表示/非表示と書いたのが悪かったかもしれません。表示です。チェックマークはどうやって(どこで)表示しているのでしょうか?
コード的には何も書いていないのです。cell.accessoryType = .checkmarkとか一切書いてないのです。なのでそもそもの使いみちが違うものなのかと…。チェックが付くだけのコードも載せたいと思います。
すみません、理解しました。チェックマークは自前で表示しているのではないんですね。こういうのがあることを知りませんでした。(ようするに、前回の回答は間違っていたということですね)
いや、コードでチェックが制御できない以上、これは使いみちがちがうんじゃないかと思っているのですが、使えたらメンテがらくになるなと。cellがタップされたときに何が行われているか監視できたりすればわかるかなと思ってみたり。
回答1件
あなたの回答
tips
プレビュー