I have a search bar above my table view. The data in table view is coming from my service. I'm trying to apply search filter on the table view data. I have tried some code but it isn't working. My code for searchbar is this,
UIViewController,UISearchBarDelegate,UITextFieldDelegate,UITextViewDelegate,ShowsAlert
@IBOutlet weak var searchBar: UISearchBar!
var filteredData = [String]()
var isSearching = false
var dishNameArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
menuView.isHidden = true
reviewView.isHidden = true
infoView.isHidden = true
scrollView.isScrollEnabled = false
//TableView Delegates
menuTableView.delegate = self
menuTableView.dataSource = self
reviewTableView.delegate = self
reviewTableView.dataSource = self
reviewTableView.reloadData()
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
segmentControl.tintColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
searchBar.delegate = self
dishNameLbl.text = name
dishDescripLbl.text = resDesc
minOrderLbl.text = minOrder
deliveryLbl.text = deliveryTime
}
private func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
isSearching = true
}
private func searchBarTextDidEndEditing(searchBar: UISearchBar) {
isSearching = false
}
private func searchBarCancelButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
private func searchBarSearchButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
private func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isSearching = false
return
}
filteredData = dishNameArray.filter({
return $0.lowercased().contains(searchText.lowercased())
})
isSearching = filteredData.count > 0
self.menuTableView.reloadData()
}
extension RestaurantMenuVC: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == menuTableView{
if isSearching{
return filteredData.count
}
return ResMenuService.instance.categoryModelInstance.count
}
else{
return AllReviewsService.instance.allReviewsModel.count
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == menuTableView{
return 57
}
else{
return 137
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == menuTableView{
let cell = menuTableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! RestaurantMenuTableViewCell
if isSearching{
cell.dishTitleLbl.text = filteredData[indexPath.row]
dishNameArray.append(cell.dishTitleLbl.text!)
}
// let cell = menuTableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! RestaurantMenuTableViewCell
cell.dishTitleLbl.text = ResMenuService.instance.categoryModelInstance[indexPath.row].categoryName
cell.cardView.layer.cornerRadius = 5
cell.selectionStyle = .none
return cell
}
else
{
let cell = reviewTableView.dequeueReusableCell(withIdentifier: "reviewCell", for: indexPath) as! AllReviewsTableViewCell
cell.nameLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].name
cell.descriptionLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].description
cell.timeLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].time
cell.ratingView.rating = Double(AllReviewsService.instance.allReviewsModel[indexPath.row].rating)
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == menuTableView{
let minimumSpending = String(ResMenuService.instance.restaurntDetailModelInstance[indexPath.row].minimumSpending)
UserDefaults.standard.set(minimumSpending, forKey: "minimumSpending")
UserDefaults.standard.synchronize()
let categoryModel = ResMenuService.instance.categoryModelInstance
let subCategoryModel = ResMenuService.instance.categoryModelInstance[indexPath.row].subCategories
let AddonCategoryModel = ResMenuService.instance.categoryModelInstance[indexPath.row].subCategories[0].items[0].addonCategory
// if categoryId == subCategoryId{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RestaurantMenuDetailVC") as! RestaurantMenuDetailVC
vc.categoryModel = categoryModel
vc.subCategoryModel = subCategoryModel
vc.AddonCategoryModel = AddonCategoryModel
self.navigationController?.pushViewController(vc, animated: true)
//}
}
else{
print("Hello")
}
}
}
But when i type something it does not filter the data. here is my model class,
struct RestaurantDetailModel {
public private(set) var restaurantId:String!
public private(set) var shopLat:String!
public private(set) var shopLng:String!
public private(set) var street:String!
public private(set) var town:String!
public private(set) var zipCode:String!
public private(set) var cellNo:String!
public private(set) var landLine:Int!
public private(set) var shopName:String!
public private(set) var deliveryTime:Int!
public private(set) var collectionTime:Int!
public private(set) var facebookLink:String!
public private(set) var twitterLink:String!
public private(set) var googleLink:String!
public private(set) var instagramLink:String!
public private(set) var pinterestLink:String!
public private(set) var address:String!
public private(set) var preorderPref:String!
public private(set) var orderStatus:Bool!
public private(set) var minimumSpending:Int!
public private(set) var restaurantTimings:[RestaurantTimingsModel]!
}