LoginSignup
kokidddd
@kokidddd (fasdfas asdfasdf)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

tableviewに検索バーを実装する方法

解決したいこと

tableviewにsearchbarを実装したいのですが、うまくいきません。調べると、自分で配列を作成してその中のものを検索するやり方は出てくるのですが、firebaeから持ってきた情報をtabelviewへ反映させている場合の検索の方法が見当たらずに困っています。どのようにsearchbarをつければいいのか教えていただけると幸いです。

class roomlist:UIViewController{

    private let cellId = "cellId"
    private var rooms = [Room]()
    @IBOutlet weak var roomlisttable: UITableView!
    @IBOutlet weak var roomsearch: UISearchBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        roomlisttable.delegate = self
        roomlisttable.dataSource = self
        fechroominfofirestore()
    }

    private func fechroominfofirestore(){
        let name: String = userDefaults.string(forKey: "name")!
        Firestore.firestore().collection("rooms").document(name).collection("detail").getDocuments{(snapshots,error) in

            if error != nil{
                print("sippai")
                return
            }
                self.rooms = []
                snapshots?.documents.forEach { snapshot in
                    let id = snapshot.documentID
                    let dic = snapshot.data()
                    let room = Room(dic: dic)
                    self.rooms.append(room)
                }
                self.roomlisttable.reloadData()
                self.rooms.forEach{(rooms) in
                }
            }   
        }
    }

extension roomlist:UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rooms.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = roomlisttable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! roomlistcell
        cell.room = rooms[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        roomlisttable.deselectRow(at: indexPath, animated: true)
        Firestore.firestore().collection("rooms").getDocuments() { (querySnapshot, err) in
            if err != nil {

            } else {
                for doc in querySnapshot!.documents {
                    let documentID = doc.documentID

                }
                }
            }
        }
0

No Answers yet.

Your answer might help someone💌