searchbarを使用して使用してtableviewに表示されているCellの検索を行いたいです。
firebaseの情報をtableviewに反映させたのですが、この検索を行おうとしてもうまくできません。Cellにはfirebaseの中に入っている情報を表示しています。searchbarより、何らかの単語を検索した際にヒットするものを表示できるようにしたいと考えています。
Swift
1import UIKit 2import Firebase 3 4class roomlist:UIViewController{ 5 6 private let cellId = "cellId" 7 private var rooms = [Room]() 8 var gotroom = [Room]() 9 private var selectedRoom : Room? 10 11 12 @IBOutlet weak var roomlisttable: UITableView! 13 @IBOutlet weak var roomsearch: UISearchBar! 14 15 var searchroom = [String]() 16 17 18 override func viewDidLoad() { 19 super.viewDidLoad() 20 21 roomlisttable.delegate = self 22 roomlisttable.dataSource = self 23 fechroominfofirestore() 24 25 } 26 27 struct PasswordData { 28 var documentID: String 29 30 } 31 private func fechroominfofirestore(){ 32 33 Firestore.firestore().collection("rooms").getDocuments{(snapshots,error) in 34 if error != nil{ 35 print("sippai") 36 37 return 38 39 } 40 41 self.rooms = [] 42 snapshots?.documents.forEach { snapshot in 43 let id = snapshot.documentID 44 let dic = snapshot.data() 45 let room = Room(dic: dic) 46 47 self.rooms.append(room) 48 } 49 self.roomlisttable.reloadData() 50 51 self.rooms.forEach{(rooms) in 52 53 } 54 } 55 56 } 57 } 58 59extension roomlist:UITableViewDelegate, UITableViewDataSource{ 60 61 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 62 } 63 // 検索バーに入力があったら呼ばれる 64 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 65 guard !searchText.isEmpty else { 66 gotroom = rooms 67 roomlisttable.reloadData() 68 return 69 } 70 gotroom = rooms.filter({ item -> Bool in 71 item.bsubname.lowercased().contains(searchText.lowercased()) 72 }) 73 roomlisttable.reloadData() 74 } 75 76 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 77 return rooms.count 78 } 79 80 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 81 let cell = roomlisttable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! roomlistcell 82 cell.room = rooms[indexPath.row] 83 84 return cell 85 } 86 87 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 88 89 print("didSelectRowAt") 90 tableView.deselectRow(at: indexPath, animated: false) 91 92 Firestore.firestore().collection("rooms").getDocuments() { (querySnapshot, err) in 93 if err != nil { 94 } else { 95 for doc in querySnapshot!.documents { 96 let documentID = doc.documentID 97 98 99 print(documentID) 100 } 101 } 102 } 103 } 104 func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 105 106 } 107 108 override func prepare(for segue : UIStoryboardSegue, sender: Any?){ 109 110 print("Will move to next view controller.") 111 switch segue.identifier { 112 case "RoomdetqailsViewController": 113 if let RoomdetqailsViewController = segue.destination as? RoomdetqailsViewController { 114 if let indexPath = self.roomlisttable.indexPathForSelectedRow { 115 let room = self.rooms[indexPath.row] 116 RoomdetqailsViewController.received = room 117 } 118 } 119 default: 120 break 121 } 122 } 123} 124 125class roomlistcell : UITableViewCell{ 126 127 var room: Room?{ 128 didSet{ 129 roomname.text = room?.bsubname 130 tname.text = "(" + room!.ctln + room!.dtfn + ")" 131 } 132 } 133 134 @IBOutlet weak var tname: UILabel! 135 @IBOutlet weak var roomimage: UIImageView! 136 @IBOutlet weak var roomname: UILabel! 137 override func awakeFromNib() { 138 super.awakeFromNib() 139 roomimage.layer.borderWidth = 0.5 140 roomimage.layer.borderColor = UIColor.bnw.cgColor 141 roomimage.layer.cornerRadius = roomimage.frame.width/2 142 } 143 override func setSelected(_ selected: Bool, animated: Bool) { 144 super.setSelected(selected, animated: animated) 145 } 146 } 147
あなたの回答
tips
プレビュー