いつもお世話になります。
UITableViewにロングプレスレコグナイザを追加して、
ロングプレス1回目のセルを始点、ロングプレス2回目のセルを終点として範囲を指定して複数セルを選択しているのですが、
選択範囲を明示するためセルを選択状態にすると、終点のセルだけが選択状態にならず困っています。
↓の画像で言うと"めだか"の選択状態が解除されてしまい、
コード上?では5個のセルが選択されているはずなのに、画面上では4個のセルしか選択されません。
解決方法はないでしょうか?
swift
1 // ロングタップした時の処理 2 @IBAction func cellLongPressed(_ sender: UILongPressGestureRecognizer) { 3 4 // 押された位置でcellのindexPathを取得 5 let point = sender.location(in: wordChainTableView) 6 let pressedIndexPath = wordChainTableView.indexPathForRow(at: point) 7 8 // 空白はタップしないでね 9 if pressedIndexPath != nil { 10 11 if sender.state == UIGestureRecognizerState.began { 12 } else if (sender.state == UIGestureRecognizerState.ended) { 13 14 // 2回めタップのときの処理 15 if selectAbleStatus == true { 16 17 labelB.text = "2回目ロングタップ" 18 19 // インデックスの順番を求める 20 let tempIndexPathA: IndexPath = pressedIndexPath! 21 let tempIndexPathB: IndexPath = targetIndexPath 22 let startIndexPath: IndexPath 23 let endIndexPath: IndexPath 24 25 // 先頭のindexPathを特定する 26 if tempIndexPathA < tempIndexPathB { 27 startIndexPath = tempIndexPathA 28 endIndexPath = tempIndexPathB 29 } else { 30 startIndexPath = tempIndexPathB 31 endIndexPath = tempIndexPathA 32 } 33 34 // 仕方がないのでイントにする 35 let sInt: Int = startIndexPath.row 36 let eInt: Int = endIndexPath.row 37 38 // 複数選択を今だけ許可 39 wordChainTableView.allowsMultipleSelection = true 40 41 for countInt in sInt...eInt { 42 let targetIndex: IndexPath = [0, countInt] 43 wordChainTableView.selectRow(at:targetIndex, animated: true, scrollPosition: .none) 44 } 45 46 // 解除して 47 // wordChainTableView.deselectRow(at: endIndexPath, animated: true) 48 // 再選択 49 // wordChainTableView.selectRow(at: endIndexPath, animated: true, scrollPosition: .none) 50 51 52 let dataCount: Int = Int(endIndexPath.row) - Int(startIndexPath.row) + 1 53 // アラートを表示 54 let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 55 alert.title = "複数行データ選択操作" 56 alert.message = "(dataCount)件のデータが選択されています。" 57 alert.addAction( 58 UIAlertAction(title: "OK", style: .cancel, handler: {(action) -> Void in self.setDefault ()})) 59 self.present(alert, animated: true, completion: nil) 60 61 // 一回目のロングタップでインデックスを取得 62 } else { 63 selectAbleStatus = true 64 targetIndexPath = pressedIndexPath! 65 labelA.text = "1回目ロングタップ" 66 } 67 } 68 } 69 } 70
サンプル:https://github.com/tyobigoro/testMultiSelectTableViewCell
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。