firebaseの情報を反映させたswifttのtableviewでタップされたセルのドキュメントidを取得する方法を教えてください。
swift
1コード 2import UIKit 3import Firebase 4 5class roomlist:UIViewController{ 6 private let cellId = "cellId" 7 private var rooms = [Room]() 8 private var selectedRoom : Room? 9 10 @IBOutlet weak var roomlisttable: UITableView! 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 roomlisttable.delegate = self 16 roomlisttable.dataSource = self 17 fechroominfofirestore() 18 19 } 20 private func fechroominfofirestore(){ 21 22 Firestore.firestore().collection("rooms").getDocuments{(snapshots,error) in 23// print("uuuuuuuuu",Firestore.firestore().collection("rooms").document("")) 24 25 if error != nil{ 26 print("sippai") 27 28 return 29 30 } 31 snapshots?.documents.forEach({ (snapshots) in 32 let dic = snapshots.data() 33 let room = Room.init(dic: dic) 34 35 self.rooms.append(room) 36 self.roomlisttable.reloadData() 37 38 self.rooms.forEach{(rooms) in 39 print("room",room.bsubname) 40 41 } 42 }) 43 44 } 45 } 46} 47extension roomlist:UITableViewDelegate, UITableViewDataSource{ 48 49 50 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 51 return rooms.count 52 } 53 54 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 55 let cell = roomlisttable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! roomlistcell 56 cell.room = rooms[indexPath.row] 57 58 return cell 59 } 60 61 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 62 63 print(indexPath) 64 print("didSelectRowAt") 65 tableView.deselectRow(at: indexPath, animated: false) 66 67 Firestore.firestore().collection("rooms").getDocuments() { (querySnapshot, err) in 68 if let err = err { 69 print("Error getting documents: (err)") 70 } else { 71 for doc in querySnapshot!.documents { 72 let documentID = doc.documentID 73 74 print(documentID) 75 } 76 } 77 } 78 79 80 81 } 82 83 func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 84 85 } 86 87 override func prepare(for segue : UIStoryboardSegue, sender: Any?){ 88 89 print("Will move to next view controller.") 90 switch segue.identifier { 91 case "RoomdetqailsViewController": 92 if let RoomdetqailsViewController = segue.destination as? RoomdetqailsViewController { 93 print("Decided a destination as RoomdetqailsViewController.") 94 if let indexPath = self.roomlisttable.indexPathForSelectedRow { 95 let room = self.rooms[indexPath.row] 96 RoomdetqailsViewController.received = room 97 } 98 } 99 default: 100 print("There is no identifier of segue.") 101 break 102 } 103 } 104} 105class roomlistcell : UITableViewCell{ 106 107 var room: Room?{ 108 didSet{ 109 roomname.text = room?.bsubname 110 } 111 } 112 113 @IBOutlet weak var roomimage: UIImageView! 114 @IBOutlet weak var roomname: UILabel! 115 override func awakeFromNib() { 116 super.awakeFromNib() 117 118 roomimage.layer.cornerRadius = roomimage.frame.width/2 119 } 120 override func setSelected(_ selected: Bool, animated: Bool) { 121 super.setSelected(selected, animated: animated) 122 } 123 }
このコードで全てのcellのドキュメントを取得する方法は実装できたのですが、タップされたcellだけのドキュメントidを取得する方法がわかたずに困っています。