前提・実現したいこと
tableCellを押した瞬間にdialogが出てきて、はいを押すとFirebase機能の delete機能で、
tableCellが消えるはずなんですけど何故か消えません。何方か原因を教えてくれると助かります。
該当のソースコード
Swift
1extension MedicineViewController:MedicineViewDelegate { 2func photoButtonTaped() { 3 4 let PhotoViewController = self.storyboard?.instantiateViewController(withIdentifier: "PhotoViewController") as! MedicineViewController 5 self.navigationController?.pushViewController( PhotoViewController, animated: true) 6 } 7} 8 9 10class MedicineViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UIGestureRecognizerDelegate{ 11 12 var medicineDataList:[MedicineData] = [] 13 14 var searchDataList:[MedicineData] = [] 15 16 17 private let db = Firestore.firestore() 18 19 20 let image = UIImage(named: "no image icon") 21 22 23 24 25 26 27 28 @IBOutlet weak var medicineSearchBar: UISearchBar! 29 30 @IBOutlet weak var tableView: UITableView! 31 32 @IBOutlet weak var AddPageButton: UIButton! 33 34 let userID = Auth.auth().currentUser?.uid 35 36 var startingFrame : CGRect! 37 var endingFrame : CGRect! 38 39 40 func scrollViewDidScroll(_ scrollView: UIScrollView) { 41 42 if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) && self.AddPageButton.isHidden { 43 44 self.AddPageButton.isHidden = false 45 46 self.AddPageButton.frame = startingFrame 47 48 UIView.animate(withDuration: 1.0) { 49 50 self.AddPageButton.frame = self.endingFrame 51 } 52 } 53 } 54 55 func configureSizes() { 56 let screenSize = UIScreen.main.bounds 57 let screenWidth = screenSize.width 58 let screenHeight = screenSize.height 59 startingFrame = CGRect(x: 0, y: screenHeight+100, width: screenWidth, height: 100) 60 endingFrame = CGRect(x: 0, y: screenHeight-100, width: screenWidth, height: 100) 61 62 } 63 64 65 private func getData(){ 66 //Firebaseのデータを取得してそれを配列にする 67 let Ref = Firestore.firestore().collection("users").document(userID!).collection("medicinedata") 68 69 Ref.getDocuments(){(data, error) 70 in if let error = error{ 71 print(error) 72 return 73 } 74 //querySnapshotにドキュメントデータが配列になって入っている。 75 76 self.medicineDataList = data!.documents.map { document in 77 let setdata = MedicineData(doc: document) 78 print(self.medicineDataList) 79 return setdata 80 } 81 //tableViewを更新 82 self.tableView.reloadData() 83 } 84 } 85 86 87 88 89 override func viewDidLoad() { 90 super.viewDidLoad() 91 getData() 92 AddPageButton.layer.cornerRadius = 32 93 medicineSearchBar.delegate = self 94 medicineSearchBar.placeholder = "診察科目を検索" 95 searchDataList = medicineDataList 96 tableView.tableFooterView = UIView() 97 tableView.delegate = self 98 tableView.dataSource = self 99 tableView.keyboardDismissMode = .onDrag 100 tableView.register(UINib(nibName:"CustomTableViewCell",bundle:nil), forCellReuseIdentifier: "Cell") 101 let longPressRecognizer = UILongPressGestureRecognizer(target: self, action:#selector(cellLongPressed)) 102 longPressRecognizer.delegate = self 103 tableView.addGestureRecognizer(longPressRecognizer) 104 self.navigationItem.hidesBackButton = true 105 } 106 107 108 override func viewWillAppear(_ animated: Bool) { 109 super.viewWillAppear(animated) 110 tableView.keyboardDismissMode = .onDrag 111 getData() 112 } 113 114 @objc func cellLongPressed(sender: UILongPressGestureRecognizer) { 115 116 let point:CGPoint = sender.location(in: tableView) 117 let indexPath = tableView.indexPathForRow(at: point) 118 let hospital = medicineDataList[indexPath!.row].hospitalData! 119 let time = medicineDataList[indexPath!.row].time! 120 121 if indexPath != nil { 122 if sender.state == UIGestureRecognizer.State.began { 123 124 let dialog = UIAlertController(title:"(String(describing: hospital))を削除しますか?", message:nil, preferredStyle: .alert) 125 let yesAction = UIAlertAction(title: "はい", style: .default, handler:{ 126 action in 127 self.db.collection("users").document("(self.userID!)").collection("medicinedata").document("(String(describing: time))").delete(){ err in 128 if let err = err { 129 print("削除できませんでした:(err)") 130 }else { 131 print("削除しました。") 132 } 133 } 134 self.tableView.reloadData() 135 }) 136 137 let noAction = UIAlertAction(title: "いいえ", style: .default, handler:nil) 138 139 dialog.addAction(yesAction) 140 dialog.addAction(noAction) 141 142 self.present(dialog, animated: true, completion: nil) 143 } 144 145 } 146 self.tableView.reloadData() 147 } 148 //画面をタッチしたらキーボードを閉じる 149 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 150 self.view.endEditing(true) 151 } 152 //検索を押したときの処理 153 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 154 searchBar.resignFirstResponder() 155 self.view.endEditing(true) 156 searchBar.showsCancelButton = true 157 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 158 //検索機能 159 if let text = self.medicineSearchBar.text { 160 if text == "" { 161 self.searchDataList = self.medicineDataList 162 self.tableView.reloadData() 163 }else { 164 self.searchDataList = self.medicineDataList.filter{ (data) -> Bool in 165 return (data.examinationData?.contains(text))! 166 } 167 self.tableView.reloadData() 168 } 169 } 170 } 171 } 172 //キャンセルボタンの処理 173 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 174 searchBar.showsCancelButton = false 175 self.view.endEditing(true) 176 medicineSearchBar.text = "" 177 self.tableView.reloadData() 178 } 179 //キャンセルボタンを表示させる処理 180 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 181 searchBar.showsCancelButton = true 182 return true 183 } 184 185 186 187 188 189 190 @IBAction func AddPageButton(_ sender: Any) { 191 let AddViewController = self.storyboard?.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController 192 self.navigationController?.pushViewController(AddViewController, animated: true) 193 } 194 195 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 196 if medicineSearchBar.text! == ""{ 197 return medicineDataList.count 198 }else { 199 return searchDataList.count 200 } 201 } 202 203 204 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 205 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell 206 207 208 if medicineSearchBar.text! == ""{ 209 let medicalList = self.medicineDataList[indexPath.row] 210 211 cell.examinationName.text = medicalList.examinationData 212 cell.hospitalName.text = medicalList.hospitalData 213 cell.timeLabel.text = medicalList.time 214 cell.imagePageButton.setBackgroundImage != nil ? cell.imagePageButton.setBackgroundImage(UIImage(named: "(String(describing: medicalList.photoData))"), for: .normal) : cell.imagePageButton.setBackgroundImage( image, for: .normal) 215 216 }else{ 217 218 let searchList = self.searchDataList[indexPath.row] 219 cell.examinationName.text = searchList.examinationData 220 cell.hospitalName.text = searchList.hospitalData 221 cell.timeLabel.text = searchList.time 222 cell.imagePageButton.image != nil ? cell.imagePageButton.setImage( UIImage(named: "(String(describing: searchList.photoData))"), for: .normal) : cell.imagePageButton.setImage( image, for: .normal) 223 } 224 return cell 225 } 226 227 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 228 return 85 229 } 230 231 232 233 } 234
あなたの回答
tips
プレビュー