Use UISearchBar with Firebase Database












1














I am trying to use UISearchBar with Firebase but I get an error when I try to type any right word





  • My Code is



    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    var isSearching: Bool = false
    //list to store all the artist
    var hotelList = [hotelsModel]()
    var filterHotels = [hotelsModel]()
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if isSearching{
    return filterHotels.count
    } else {
    return hotelList.count
    }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! hotelsTableViewCell

    //the artist object
    let hotel: hotelsModel
    //getting the artist of selected position
    hotel = hotelList[indexPath.row]

    //adding values to labels
    cell.lblName.text = hotel.name
    cell.lblLocation.text = hotel.location
    cell.appSuggest.text = hotel.appSuggest
    cell.price.text = hotel.price
    cell.canceletion.text = hotel.cancelation
    cell.paymentNeeded.text = hotel.paymentNeeded
    if isSearching{
    cell.lblName?.text = filterHotels[indexPath.row].name
    }

    return cell
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
    isSearching = false
    view.endEditing(true)
    tableView.reloadData()
    } else {
    isSearching = true
    filterHotels = hotelList.filter({$0.name == searchBar.text})
    tableView.reloadData()
    }
    }



  • in this line



    if isSearching{

    cell.lblName?.text = filterHotels[indexPath.row].name
    }


    https://i.stack.imgur.com/wSsP4.png




  • this's my code files
    if someone can check it



    https://github.com/HaMaDaRaOuF/UISearchBar-Firabse




thank you










share|improve this question
























  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
    – Frank van Puffelen
    Nov 19 '18 at 15:10










  • okay but i already upload my code in link but i'll edit my qution with my code also
    – Hamada Raouf
    Nov 19 '18 at 15:21










  • What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
    – NRitH
    Nov 19 '18 at 15:30












  • already added my code and image by error
    – Hamada Raouf
    Nov 19 '18 at 15:37
















1














I am trying to use UISearchBar with Firebase but I get an error when I try to type any right word





  • My Code is



    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    var isSearching: Bool = false
    //list to store all the artist
    var hotelList = [hotelsModel]()
    var filterHotels = [hotelsModel]()
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if isSearching{
    return filterHotels.count
    } else {
    return hotelList.count
    }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! hotelsTableViewCell

    //the artist object
    let hotel: hotelsModel
    //getting the artist of selected position
    hotel = hotelList[indexPath.row]

    //adding values to labels
    cell.lblName.text = hotel.name
    cell.lblLocation.text = hotel.location
    cell.appSuggest.text = hotel.appSuggest
    cell.price.text = hotel.price
    cell.canceletion.text = hotel.cancelation
    cell.paymentNeeded.text = hotel.paymentNeeded
    if isSearching{
    cell.lblName?.text = filterHotels[indexPath.row].name
    }

    return cell
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
    isSearching = false
    view.endEditing(true)
    tableView.reloadData()
    } else {
    isSearching = true
    filterHotels = hotelList.filter({$0.name == searchBar.text})
    tableView.reloadData()
    }
    }



  • in this line



    if isSearching{

    cell.lblName?.text = filterHotels[indexPath.row].name
    }


    https://i.stack.imgur.com/wSsP4.png




  • this's my code files
    if someone can check it



    https://github.com/HaMaDaRaOuF/UISearchBar-Firabse




thank you










share|improve this question
























  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
    – Frank van Puffelen
    Nov 19 '18 at 15:10










  • okay but i already upload my code in link but i'll edit my qution with my code also
    – Hamada Raouf
    Nov 19 '18 at 15:21










  • What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
    – NRitH
    Nov 19 '18 at 15:30












  • already added my code and image by error
    – Hamada Raouf
    Nov 19 '18 at 15:37














1












1








1







I am trying to use UISearchBar with Firebase but I get an error when I try to type any right word





  • My Code is



    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    var isSearching: Bool = false
    //list to store all the artist
    var hotelList = [hotelsModel]()
    var filterHotels = [hotelsModel]()
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if isSearching{
    return filterHotels.count
    } else {
    return hotelList.count
    }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! hotelsTableViewCell

    //the artist object
    let hotel: hotelsModel
    //getting the artist of selected position
    hotel = hotelList[indexPath.row]

    //adding values to labels
    cell.lblName.text = hotel.name
    cell.lblLocation.text = hotel.location
    cell.appSuggest.text = hotel.appSuggest
    cell.price.text = hotel.price
    cell.canceletion.text = hotel.cancelation
    cell.paymentNeeded.text = hotel.paymentNeeded
    if isSearching{
    cell.lblName?.text = filterHotels[indexPath.row].name
    }

    return cell
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
    isSearching = false
    view.endEditing(true)
    tableView.reloadData()
    } else {
    isSearching = true
    filterHotels = hotelList.filter({$0.name == searchBar.text})
    tableView.reloadData()
    }
    }



  • in this line



    if isSearching{

    cell.lblName?.text = filterHotels[indexPath.row].name
    }


    https://i.stack.imgur.com/wSsP4.png




  • this's my code files
    if someone can check it



    https://github.com/HaMaDaRaOuF/UISearchBar-Firabse




thank you










share|improve this question















I am trying to use UISearchBar with Firebase but I get an error when I try to type any right word





  • My Code is



    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    var isSearching: Bool = false
    //list to store all the artist
    var hotelList = [hotelsModel]()
    var filterHotels = [hotelsModel]()
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if isSearching{
    return filterHotels.count
    } else {
    return hotelList.count
    }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! hotelsTableViewCell

    //the artist object
    let hotel: hotelsModel
    //getting the artist of selected position
    hotel = hotelList[indexPath.row]

    //adding values to labels
    cell.lblName.text = hotel.name
    cell.lblLocation.text = hotel.location
    cell.appSuggest.text = hotel.appSuggest
    cell.price.text = hotel.price
    cell.canceletion.text = hotel.cancelation
    cell.paymentNeeded.text = hotel.paymentNeeded
    if isSearching{
    cell.lblName?.text = filterHotels[indexPath.row].name
    }

    return cell
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
    isSearching = false
    view.endEditing(true)
    tableView.reloadData()
    } else {
    isSearching = true
    filterHotels = hotelList.filter({$0.name == searchBar.text})
    tableView.reloadData()
    }
    }



  • in this line



    if isSearching{

    cell.lblName?.text = filterHotels[indexPath.row].name
    }


    https://i.stack.imgur.com/wSsP4.png




  • this's my code files
    if someone can check it



    https://github.com/HaMaDaRaOuF/UISearchBar-Firabse




thank you







swift firebase uisearchbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:09

























asked Nov 19 '18 at 15:07









Hamada Raouf

114




114












  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
    – Frank van Puffelen
    Nov 19 '18 at 15:10










  • okay but i already upload my code in link but i'll edit my qution with my code also
    – Hamada Raouf
    Nov 19 '18 at 15:21










  • What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
    – NRitH
    Nov 19 '18 at 15:30












  • already added my code and image by error
    – Hamada Raouf
    Nov 19 '18 at 15:37


















  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
    – Frank van Puffelen
    Nov 19 '18 at 15:10










  • okay but i already upload my code in link but i'll edit my qution with my code also
    – Hamada Raouf
    Nov 19 '18 at 15:21










  • What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
    – NRitH
    Nov 19 '18 at 15:30












  • already added my code and image by error
    – Hamada Raouf
    Nov 19 '18 at 15:37
















"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 19 '18 at 15:10




"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 19 '18 at 15:10












okay but i already upload my code in link but i'll edit my qution with my code also
– Hamada Raouf
Nov 19 '18 at 15:21




okay but i already upload my code in link but i'll edit my qution with my code also
– Hamada Raouf
Nov 19 '18 at 15:21












What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
– NRitH
Nov 19 '18 at 15:30






What's the error? Nobody's going to download, build, and run unfamiliar code just to see the error that you could have copied & pasted here.
– NRitH
Nov 19 '18 at 15:30














already added my code and image by error
– Hamada Raouf
Nov 19 '18 at 15:37




already added my code and image by error
– Hamada Raouf
Nov 19 '18 at 15:37












2 Answers
2






active

oldest

votes


















1














I will suggest you to change the filter process (smth like this):



func filterContentForSearchText(_ searchText: String) {

let pred = NSPredicate(format: "name contains[cd] %@", searchText)
filteredHotels = hotelList?.filtered(using: pred) as? [hotelsModel]

tableView.reloadData()
}


and change the isSearching variable to this:



var isSearching: Bool {
return searchBar.text != ""
}


Use the debugger to see the indexPath.row value on the line that is causing your crash.






share|improve this answer





















  • Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
    – Hamada Raouf
    Nov 19 '18 at 16:11



















0














Thank You guys my problem it's was in the Number Of Rows In Section Function it's haven't return for filterHotels I edit my question if someone need the code also i'll edit it in github



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }



  • Github Link
    https://github.com/HaMaDaRaOuF/UISearchBar-Firabse






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377464%2fuse-uisearchbar-with-firebase-database%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I will suggest you to change the filter process (smth like this):



    func filterContentForSearchText(_ searchText: String) {

    let pred = NSPredicate(format: "name contains[cd] %@", searchText)
    filteredHotels = hotelList?.filtered(using: pred) as? [hotelsModel]

    tableView.reloadData()
    }


    and change the isSearching variable to this:



    var isSearching: Bool {
    return searchBar.text != ""
    }


    Use the debugger to see the indexPath.row value on the line that is causing your crash.






    share|improve this answer





















    • Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
      – Hamada Raouf
      Nov 19 '18 at 16:11
















    1














    I will suggest you to change the filter process (smth like this):



    func filterContentForSearchText(_ searchText: String) {

    let pred = NSPredicate(format: "name contains[cd] %@", searchText)
    filteredHotels = hotelList?.filtered(using: pred) as? [hotelsModel]

    tableView.reloadData()
    }


    and change the isSearching variable to this:



    var isSearching: Bool {
    return searchBar.text != ""
    }


    Use the debugger to see the indexPath.row value on the line that is causing your crash.






    share|improve this answer





















    • Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
      – Hamada Raouf
      Nov 19 '18 at 16:11














    1












    1








    1






    I will suggest you to change the filter process (smth like this):



    func filterContentForSearchText(_ searchText: String) {

    let pred = NSPredicate(format: "name contains[cd] %@", searchText)
    filteredHotels = hotelList?.filtered(using: pred) as? [hotelsModel]

    tableView.reloadData()
    }


    and change the isSearching variable to this:



    var isSearching: Bool {
    return searchBar.text != ""
    }


    Use the debugger to see the indexPath.row value on the line that is causing your crash.






    share|improve this answer












    I will suggest you to change the filter process (smth like this):



    func filterContentForSearchText(_ searchText: String) {

    let pred = NSPredicate(format: "name contains[cd] %@", searchText)
    filteredHotels = hotelList?.filtered(using: pred) as? [hotelsModel]

    tableView.reloadData()
    }


    and change the isSearching variable to this:



    var isSearching: Bool {
    return searchBar.text != ""
    }


    Use the debugger to see the indexPath.row value on the line that is causing your crash.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 15:43









    Deryck Lucian

    1547




    1547












    • Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
      – Hamada Raouf
      Nov 19 '18 at 16:11


















    • Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
      – Hamada Raouf
      Nov 19 '18 at 16:11
















    Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
    – Hamada Raouf
    Nov 19 '18 at 16:11




    Thank You gays my problim it's was in the Number Of Rows In Section Function it's havn't return for filterHotels i edit my qution if someone need the code also i'll edit it in github func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }
    – Hamada Raouf
    Nov 19 '18 at 16:11













    0














    Thank You guys my problem it's was in the Number Of Rows In Section Function it's haven't return for filterHotels I edit my question if someone need the code also i'll edit it in github



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }



    • Github Link
      https://github.com/HaMaDaRaOuF/UISearchBar-Firabse






    share|improve this answer




























      0














      Thank You guys my problem it's was in the Number Of Rows In Section Function it's haven't return for filterHotels I edit my question if someone need the code also i'll edit it in github



      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }



      • Github Link
        https://github.com/HaMaDaRaOuF/UISearchBar-Firabse






      share|improve this answer


























        0












        0








        0






        Thank You guys my problem it's was in the Number Of Rows In Section Function it's haven't return for filterHotels I edit my question if someone need the code also i'll edit it in github



        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }



        • Github Link
          https://github.com/HaMaDaRaOuF/UISearchBar-Firabse






        share|improve this answer














        Thank You guys my problem it's was in the Number Of Rows In Section Function it's haven't return for filterHotels I edit my question if someone need the code also i'll edit it in github



        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int { if isSearching{ return filterHotels.count } else { return hotelList.count } }



        • Github Link
          https://github.com/HaMaDaRaOuF/UISearchBar-Firabse







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 16:15









        NathanOliver

        87k15120180




        87k15120180










        answered Nov 19 '18 at 16:13









        Hamada Raouf

        114




        114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377464%2fuse-uisearchbar-with-firebase-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$