简体   繁体   中英

Search bar filter table view swift 4

I will try to make this as clear as possible.. i want to implement UI Search bar to filter through a table view of JSON objects. Im specifically using a Yelp api which has an autocomplete search feature. In my class, i confirmed to the UISearchBarDelegate - and from some online tutorials, i believe there are two protocols i have to implement. 1 is "func searchBar(textDidChange)" and the other i believe is "selected scope button index did change".

In the textdidchange function i am able to print out the list of searches by making a query to the autocomplete endpoint. But im stuck on how to load this data (newBizName array) so that the results are shown under the search bar as a user is typing. This is my first time implementing a search feature so im kind of stuck in the mud, any help would be appreciated. thanks

// the search bar protocol where i query the yelp endpoint - im able to get the autocomplete results as shown in the print section

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    yelpAPIClient.autocompleteBusinesses(byText: searchText, latitude: Double(Location.sharedInstance.latitude), longitude: Double(Location.sharedInstance.longitude), locale: CDYelpLocale.english_unitedStates) { (response) in

        if let response = response,
            let businesses = response.businesses,
            businesses.count > 0 {

            for business in (response.businesses)! {

                self.filteredBusinesses.append(business.name!)

            }
            self.yelpTableView.reloadData()

        }
        self.newBizName = self.filteredBusinesses
        print(self.newBizName)
        //prints after pressing P - ["Panera Bread", "Popeyes Louisiana Kitchen", "P.F. Chang\'s"]

    }


    }

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {

}

func isFiltering() -> Bool {
    return searchController.isActive && !searchBarIsEmpty()

}

       // THE ORIGINAL TABLE VIEW THAT APPEARS UPON VIEW LOADING. 
    //I NEED TO GET THE FILTERED RESULTS INTO 
//THIS AREA AFTER THE USER CLICKS ONTO THE SEARCH BAR
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {





return (self.nameArray.count)

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "YelpCell") as? YelpCell else { return UITableViewCell() }

    cell.YelpNameLbl.text = nameArray[indexPath.row]
    cell.yelpAddressLbl.text = locationArray[indexPath.row]
    cell.yelpImg.downloadedFrom(url: yelpURLS[indexPath.row]!)
    cell.yelpDollarSignLbl.text = yelpDollarsSigns[indexPath.row]
    cell.distanceFromYou.text = String(format: "%.2f", distanceFromYou[indexPath.row]!)



    return cell

}

I think you need to reload the table like this:

DispatchQueue.main.async { 
     self.yelpTableView.reloadData()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM