2017-08-02 39 views
1

我是iOS開發的初學者,當我嘗試在UITableView中使用UISearchBar時遇到問題。 我不知道如何在使用搜索欄過濾數據的表格視圖中使用自定義類。請,你能解決這個問題嗎?下面是自定義類:帶自定義類的SearchBar

// Data.swift 
// SearchBarWithTableView 

class Food { 
var FoodName:String = "" 
var FoodQuantity:String = "" 
var FoodGroup:String = "" 
var FoodImage:String = "" 
var FoodCarbs:Int = 0 
var FoodCalories:Int = 0 

init(foodName:String,foodGroup:String,foodQuantity:String,foodImage:String,foodCarbs:Int,foodCalories:Int) { 
    self.FoodName = foodName 
    self.FoodQuantity = foodQuantity 
    self.FoodGroup = foodGroup 
    self.FoodImage = foodImage 
    self.FoodCarbs = foodCarbs 
    self.FoodCalories = foodCalories 
}} 

ViewController.swift

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{ 

    @IBOutlet weak var SearchBarTableView:UITableView! 
    @IBOutlet weak var SearchBar:UISearchBar! 




    var searchActive:Bool=false 
    var filteredData=[Food]() 

    let allData:[Food]=[Food(foodName: "Rice", foodGroup: "Beans", foodQuantity: "5 scope",foodImage:"1S.jpg", foodCarbs:15,foodCalories: 80), 
         Food(foodName: "Apple", foodGroup: "Frutie", foodQuantity: "One",foodImage:"S-2.jpg", foodCarbs: 15, foodCalories: 80), 
         Food(foodName: "ban 1",foodGroup: "Beans",foodQuantity:"One",foodImage:"3S.jpg",foodCarbs: 15,foodCalories: 80), 
         Food(foodName: "ban 2", foodGroup: "Beans", foodQuantity: "half pieace",foodImage:"4-S.jpg", foodCarbs: 25, foodCalories: 140)] 



    override func viewDidLoad() { 

     super.viewDidLoad() 

     SearchBarTableView.delegate=self 
     SearchBarTableView.dataSource=self 
     SearchBar.delegate=self 
     filteredData=allData 
     print(filteredData.count) 
     print("-----------") 
     print(allData.count) 
     SearchBar.returnKeyType=UIReturnKeyType.done 
     definesPresentationContext=true 
    } 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell=tableView.dequeueReusableCell(withIdentifier: "cell") 
       var Text:String 
       if searchActive { 
        Text=filteredData[indexPath.row].FoodName 
       }else{ 
        Text=allData[indexPath.row].FoodName 
       } 
       cell?.textLabel?.text=filteredData[indexPath.row].FoodName 
       return cell! 
      } 

     func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {` 
      if SearchBar.text == nil || SearchBar.text == "" { 
       searchActive=false 
       view.endEditing(true) 
       SearchBarTableView.reloadData() 
      }else{ 
       searchActive=true 
    /* here is the problem */ 
       // filteredData=allData.filter({$0 === searchBar.text!}) 
       } 
      } 
     func numberOfSections(in tableView: UITableView) -> Int { 
      return 1 
     } 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      if searchActive { 
       return filteredData.count 
      } 
      return allData.count 
     } 

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
      if segue.identifier=="Go"{ 
      if let indexPath=SearchBarTableView.indexPathForSelectedRow{ 
       let destinationVC=segue.destination as! DetailsViewController 
       destinationVC.namE=allData[indexPath.row].FoodName 
       destinationVC.quntitY=allData[indexPath.row].FoodQuantity 
       destinationVC.calorieS=allData[indexPath.row].FoodCalories 
       destinationVC.carbS=allData[indexPath.row].FoodCarbs 
      } 
     } 
     } 
    } 

回答

0
func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {` 
     if SearchBar.text == nil || SearchBar.text == "" { 
      searchActive=false 
      view.endEditing(true) 
      SearchBarTableView.reloadData() 
     }else{ 
      searchActive=true 

       filteredData=allData.filter({$0.FoodName === searchBar.text!}) 
      SearchBarTableView.reloadData() 


      } 
     } 
0
func updateSearchResults(for searchController: UISearchController) { 
    if let searchText = searchController.searchBar.text, !searchText.isEmpty { 
     self.filteredData = self.allData.filter{ 
      var found = false 
      for str in ($0 as! NSDictionary) { 
       if(str.key as! String == "foodName") 
       { 
        var strin = str.value as! String; 
        strin = strin.lowercased(); 
        let search = searchText.lowercased() 
        found = strin.contains(search); 
       }      
      } 
      return found 
     } 
    } 
    else { 
     self.Array = response as! [Any] 
    } 
    tableView.reloadData() 
} 

使用上述方法。