2017-12-01 18 views
0

私は7つのカテゴリAll、A、B、C、D、E、Fを持つplistを作成しました。各カテゴリにはアイテムのリストがあります。今私はセグメント化されたコントロールを使用しようとしているので、ユーザーがAをクリックすると、テーブルビューのAカテゴリにリストされているすべての項目が表示されます。セグメント化されたコントロールを使用して、私のplistのすべてのカテゴリを表示するにはどうすればよいですか?

これまでのところ、このように私のコードの外観は記載されているアイテムを取得する:

override func viewDidLoad() { 
super.viewDidLoad() 

// Setup the Search Controller 
searchController.searchResultsUpdater = self 
searchController.obscuresBackgroundDuringPresentation = false 
searchController.searchBar.placeholder = "Search Summons" 
navigationItem.searchController = searchController 
navigationItem.hidesSearchBarWhenScrolling = false 
definesPresentationContext = true 

// Setup the Scope Bar 
searchController.searchBar.scopeButtonTitles = ["All", "A", "B", "C", "D", "E", "F"] 
searchController.searchBar.delegate = self 

// Setup the search footer 
tableView.tableFooterView = searchFooter 

if let splitViewController = splitViewController { 
let controllers = splitViewController.viewControllers 
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController 
} 
    //Load Plist File 
    let path = Bundle.main.path(forResource:"Summonses", ofType: "plist") 
    let dics = NSArray.init(contentsOf: URL.init(fileURLWithPath:path!)) as! [NSDictionary] 


    self.originalData = dics.map{Summonses.init(category: $0["category"] as! String, price: $0["price"] as! String, description: $0["description"] as! String, accusatory: $0["accusatory"] as! String, name: $0["name"] as! String, info: $0["info"] as! String)} 

    self.filteredData = self.originalData 


} 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if isFiltering() { 

      searchFooter.setIsFilteringToShow(filteredItemCount: filteredData.count, of: originalData.count) 
     return filteredData.count 
     } 
     switch (Controller.selectedSegmentIndex){ 
     case 0: 
      return filteredData.count 
     case 1: 
      //Not exactly sure what to put in this section to count for the items in category A 
      //return filteredData.category["A"].count <--- something like that 
     default: 
      break 
     } 

それはそれをしないが、私はそれがセグメント化された制御で動作するように取得するように見えるカント私のスコープの検索バーのよう。 「All」、「A」、「B」、「C」、「D」、「E」、「F」の文字列が含まれており、ユーザがヒットAカテゴリーAのすべてのアイテムが表示されます。上に自分のコードにコメントをつけた私は、そのカテゴリにAを持つすべてのアイテムを数えるために、その場所に置くコードの種類はわかりません。

答えて

0

セグメントインデックスを使用してすべてのカテゴリを読み込むには、次のコードを使用してください。必要に応じてcellForRowAt indexPathを変更します。

struct Summonses { 
    var category:String 
    var price:String 
    var description:String 
    var accusatory:String 
    var name :String 
    var info:String 
} 
class CategoryViewController: UITableViewController,UISearchResultsUpdating ,UISearchBarDelegate{ 


    let searchController = UISearchController(searchResultsController: nil) 

    let categories = ["All", "A", "B", "C", "D", "E", "F"] 
    var originalData = [Summonses]() 
    var filteredData = [Summonses]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     searchController.searchResultsUpdater = self 
     searchController.obscuresBackgroundDuringPresentation = false 
     searchController.searchBar.placeholder = "Search Summons" 
     if #available(iOS 11.0, *) { 
      navigationItem.searchController = searchController 
      navigationItem.hidesSearchBarWhenScrolling = false 
     } 
     definesPresentationContext = true 

     // Setup the Scope Bar 
     searchController.searchBar.scopeButtonTitles = categories 
     searchController.searchBar.delegate = self 

     let path = Bundle.main.path(forResource:"Summonses", ofType: "plist") 
     let dics = NSArray.init(contentsOf: URL.init(fileURLWithPath:path!)) as! [NSDictionary] 

     self.originalData = dics.map{Summonses.init(category: $0["category"] as! String, price: String(describing: $0["price"]), description: $0["description"] as! String, accusatory: $0["accusatory"] as! String, name: $0["name"] as! String, info: $0["info"] as! String)} 

     self.filteredData = self.originalData 


    } 

    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) 
    { 
     print(selectedScope) 
     if selectedScope != 0 { 
      filteredData.removeAll(keepingCapacity: false) 
      let array = self.originalData.filter{$0.category.contains(categories[selectedScope])} 
      filteredData = array 
      self.tableView.reloadData() 
     } 
     else{ 
      filteredData = originalData 
      self.tableView.reloadData() 
     } 

    } 
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
     self.filteredData = self.originalData 
     self.tableView.reloadData() 

    } 

    func updateSearchResults(for searchController: UISearchController) { 
     if !((searchController.searchBar.text?.isEmpty)!){ 
      filteredData.removeAll(keepingCapacity: false) 
      let array = self.originalData.filter{$0.category.contains(searchController.searchBar.text!)} 
      filteredData = array 
      self.tableView.reloadData() 
     } 
    } 


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if self.searchController.isActive { 
      return self.filteredData.count 
     }else{ 
      return self.originalData.count 
     } 

    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
     let objSummonses : Summonses = self.filteredData[indexPath.row] 
     cell.textLabel?.text = "\(objSummonses.category) -> \(objSummonses.name)" 
     return cell 

    } 

} 
+0

ok私は自宅でコードを調べましたが、実際にコード内のどこかでselectedsegmentindexを実装することはできませんでした。スコープバーを変更したような感じでした。 – Dkeem

+0

selectedScopeButtonIndexDidChangeこのメソッドは、検索バーのセグメントコントロールのインデックスを与えます。詳細はdocを参照してください。 – Vinodh

関連する問題