user13098418
user13098418

Reputation:

How to make a search bar page that connects to Firebase in Xcode 11

I am trying to implement a search bar with a table view into my app that searches data that is in Firebase storage. I am getting many errors while making this. Two of my errors have Use of unresolved identifier 'cell', two have Use of unresolved identifier 'inSearchMode' and the next two are Value of type 'Storage' has no subscripts and Value of type 'Storage' has no member 'filter'. Ive been trying to figure out these errors for awhile now. Any assistance would be VERY appreciated! Thank you.

ps the ERRORS are shown as comments:

import Foundation

import Firebase

class SearchBarViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

var data = Storage.storage()
var filteredData = [String]()
var isSearching = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self
    searchBar.returnKeyType = UIReturnKeyType.done
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isSearching {
        return filteredData.count
    }
    return data.accessibilityElementCount()  //Might be a problem
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) is DataCell {

        let text: String!

        if isSearching {
            text = filteredData[indexPath.row]
        } else {

                text = data[indexPath.row] //Value of type 'Storage' has no subscripts
        }
        cell.configureCell(data: text) //Use of unresolved identifier 'cell'

        return cell //Use of unresolved identifier 'cell'


    } else {

        return UITableViewCell()

    }
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {

        inSearchMode = false //Use of unresolved identifier 'inSearchMode'

        view.endEditing(true)

        tableView.reloadData()
    } else {
        inSearchMode = true //Use of unresolved identifier 'inSearchMode'

        filteredData = data.filter({$0 == searchBar.text!}) //Value of type 'Storage' has no member 'filter'

        tableView.reloadData()
    }
}

}

Upvotes: 1

Views: 108

Answers (1)

noob
noob

Reputation: 555

The error Use of unresolved identifier 'cell' caused because you are not actually creating any cell, you just type check is DataCell. In order to fix this this line should be

if let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as? DataCell { 
}

for the second issue, accessing data[indexPath.row], since I don't know what data type it is, can't give you answer.

for the third issue, Use of unresolved identifier 'inSearchMode', there is no variable declared for it. So it's expected. isSearching may be the one you should replace with it.

Upvotes: 1

Related Questions