如何使用分段控件显示 plist 中的所有类别?

Posted

技术标签:

【中文标题】如何使用分段控件显示 plist 中的所有类别?【英文标题】:How can I use the Segmented Control to show all the categories from my plist? 【发布时间】:2017-12-01 02:09:08 【问题描述】:

好的,所以我创建了一个包含 7 个类别 All、A、B、C、D、E、F 的 plist。每个类别都有一个项目列表。现在我正在尝试使用分段控件,以便如果用户单击 A,它会显示 tableview 上 A 类别中列出的所有项目。

到目前为止,我的代码看起来像这样来获取列出的项目:

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.mapSummonses.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”,如果用户,我只希望这些项目显示在表格视图中hits A 类别 A 中的所有项目都显示出来。在顶部对我的代码进行了评论我不确定在该位置放置什么样的代码来计算所有在其类别中具有 A 的项目。

【问题讨论】:

【参考方案1】:

请使用以下代码使用段索引加载所有类别。根据您的要求更改 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.mapSummonses.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

    


【讨论】:

好的,所以我在家里查看了代码,我并没有真正看到您在代码中的任何地方实现 selectedsegmentindex。好像你刚刚修改了我的范围栏。 selectedScopeButtonIndexDidChange 此方法将在搜索栏中提供段控制的索引阅读文档以获取更多详细信息

以上是关于如何使用分段控件显示 plist 中的所有类别?的主要内容,如果未能解决你的问题,请参考以下文章

iOS项目开发实战——学会使用TableView列表控件plist读取与Section显示

表格视图顶部的分段控件,但不在标题视图中

iPhone sdk如何使用分段控件显示的两个不同视图设置视图方向

如何在 Swift 的工具栏顶部居中分段控件?

如何在单个视图中处理多个分段控件的事件

将 NSMutableArray 中的所有值相加并根据普通分段控制乘以值