I am trying to implement search bar with search display feature in swift for the table view given below.
Screenshot of the table view
I am getting issue because my string array is in the following format.
["section name1" : ["list1", "list2"] ,"section name2" : ["list3", "list4"]]
I have added my code below for the table view.
@IBOutlet weak var dishtable: UITableView!
@IBOutlet weak var namlbl: UILabel!
var Dishes = ["POPULAR Dishes": ["Biryani", "Tandori Chicken","Butter Chicken", "Vada Pav"],"A": ["Aloo baingan", "Aloo ki Tikki", "Amritsari fish"], "B": ["Baigan bharta", "Biryani"]];
var Filterval = ["POPULAR Dishes": ["Biryani", "Tandori Chicken","Butter Chicken", "Vada Pav"],"A": ["Aloo baingan", "Aloo ki Tikki", "Amritsari fish"], "B": ["Baigan bharta", "Biryani"]];
struct dish {
let Dish : [String]
}
override func viewDidLoad() {
super.viewDidLoad()
self.dishtable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
dishtable.dataSource = self
dishtable.delegate = self
// Do any additional setup after loading the view.
}
func filterContentForSearchText(searchText: String) {
// Filter the array using the filter method
self.Filterval = self.Dishes.filter({( d: dish) -> Bool in
let DishMatch = (d.Dish == scope)
let stringMatch = d.Dish.rangeOfString(searchText)
return DishMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!)
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
let sections:Array<AnyObject> = ["POPULAR Dishes","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var usernames = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellID = "cell"
let cell: UITableViewCell = self.dishtable.dequeueReusableCellWithIdentifier(cellID) as! UITableViewCell
println("value : \(indexPath.section)")
println("value 1: \(indexPath.row)")
var d : dish
if tableView == self.searchDisplayController!.searchResultsTableView {
cell.textLabel!.text = Filterval[sections[indexPath.section] as! String]![indexPath.row]
} else {
cell.textLabel!.text = Dishes[sections[indexPath.section] as! String]![indexPath.row]
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
println("Dishes section count : \(section)")
if tableView == self.searchDisplayController!.searchResultsTableView {
if section == 0 {
return Filterval["POPULAR Dishes"]!.count
}
else if section == 1 {
return Filterval["A"]!.count
}
else if section == 2 {
return Filterval["B"]!.count
}
else if section == 3 {
return Filterval["C"]!.count
}
else if section == 4 {
return Filterval["D"]!.count
}
else if section == 5 {
return Filterval["E"]!.count
}
else if section == 6 {
return Filterval["F"]!.count
}
return 0
} else {
if section == 0 {
return Dishes["POPULAR Dishes"]!.count
}
else if section == 1 {
return Dishes["A"]!.count
}
else if section == 2 {
return Dishes["B"]!.count
}
else if section == 3 {
return Dishes["C"]!.count
}
else if section == 4 {
return Dishes["D"]!.count
}
else if section == 5 {
return Dishes["E"]!.count
}
else if section == 6 {
return Dishes["F"]!.count
}
return 0
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 27
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView,
sectionForSectionIndexTitle title: String,
atIndex index: Int) -> Int{
return index
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return self.sections[section] as? String
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var ban = ""
ban = Dishes[sections[indexPath.section] as! String]![indexPath.row]
println(ban)
}
I have tried the following links online, but unable to implement the search feature.
http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
http://www.raywenderlich.com/76519/add-table-view-search-swift
Can someone help me with search bar?