Mariah Mariah - 7 months ago 44
Swift Question

Search Bar on TableView Does not Work

I'm trying to add a search bar to a simple table view consisting of 7 cells of names and small description for each name.

As in the image here:

enter image description here

I made a class in swift file called Business, that has two attributes: Name and Des.

Here's the code in the view controller:

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var TableView: UITableView!
var B = [Business]() //candies
var filteredNames = [Business]()

let searchController = UISearchController(searchResultsController: nil)

func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredNames = B.filter { Bu in
return Bu.Name.lowercaseString.containsString(searchText.lowercaseString)
}

TableView.reloadData()
}


override func viewDidLoad() {
super.viewDidLoad()


B = [
Business(Name:"Mariah", Des:"I'm Here to help"),
Business(Name:"Nada", Des:"Hi"),
Business(Name:"Atheer", Des:"Hello"),
Business(Name:"Lojian", Des:"I can Help you"),
Business(Name:"Nadya", Des:"Hayat"),
Business(Name:"Omnia", Des:"Yahoo"),
Business(Name:"Eman", Des:"I have amazing stuff"),
Business(Name:"Amani", Des:"Yess")
]

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
TableView.tableHeaderView = searchController.searchBar

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return filteredNames.count
}
return B.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.TableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellTableViewCell

cell.NameLabel.text = B[indexPath.row].Name
cell.DescriptionLabel.text = B[indexPath.row].Des

let Bu: Business

if searchController.active && searchController.searchBar.text != "" {
Bu = filteredNames[indexPath.row]
} else {
Bu = B[indexPath.row]
}
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}


}

extension FirstViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController:
(UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}


I followed this tutorial to do that:
https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

I don't know whay when I tried to search in simulator the result is always the first cell: Mariah

What's wrong with the code?

Answer

You don't use the search result to populate the cells. Replace you cellForRowAtIndexPath with this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.TableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellTableViewCell

    let Bu: Business

    if searchController.active && searchController.searchBar.text != "" {
        Bu = filteredNames[indexPath.row]
    } else {
        Bu = B[indexPath.row]
    }

    cell.NameLabel.text = Bu.Name
    cell.DescriptionLabel.text = Bu.Des

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    return cell
}

And, don't use capital first letters for properties.