2017-12-01 2 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가있는 모든 항목을 계산하기 위해 어떤 지점에 코드를 삽입해야할지 모르겠습니다.

답변

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

괜찮 았어. 집에서 코드를 살펴 봤는데 코드의 어느 부분에서든지 selectedsegmentindex를 구현하는 것을 정말로 보지 못했습니다. 방금 내 스코프 바를 수정 한 것처럼 보였습니다. – Dkeem

+0

selectedScopeButtonIndexDidChange이 메서드는 검색 막대의 세그먼트 컨트롤에 대한 색인을 제공하여 자세한 내용을 확인합니다. – Vinodh

관련 문제