ios - 使用 UISearchController 时如何过滤 updateSearchResultsForSearchController 中的对象数组?

标签 ios swift uisearchcontroller

我的搜索 Controller 类如下:

class FeastSearchTableViewController: UITableViewController, UISearchResultsUpdating {

    var feastEntries = [FeastEntry]()

    var filteredFeasts = [FeastEntry]()


   /* let tableData = ["One","Two","Three","Twenty-One"]
    var filteredTableData = [String]()*/
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()

  self.feastEntries = [FeastEntry(category:"Chocolate", name:"chocolate Bar"),
            FeastEntry(category:"Chocolate", name:"chocolate Chip"),
            FeastEntry(category:"Chocolate", name:"dark chocolate"),
            FeastEntry(category:"Hard", name:"lollipop"),
            FeastEntry(category:"Hard", name:"candy cane"),
            FeastEntry(category:"Hard", name:"jaw breaker"),
            FeastEntry(category:"Other", name:"caramel"),
            FeastEntry(category:"Other", name:"sour chew"),
            FeastEntry(category:"Other", name:"gummi bear")]

        // Reload the table
        self.tableView.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 2
        if (self.resultSearchController.active) {
            return self.filteredFeasts.count
        }
        else {
            return self.feastEntries.count
        }
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let feasts = self.feastEntries[indexPath.row]

            let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCellPhoto") as! TimelineCell

            cell.typeImageView.image = UIImage(named: "timeline-photo")
            cell.profileImageView.image = UIImage(named: "profile-pic-2")
            cell.nameLabel.text = feasts.name
            cell.photoImageView?.image = UIImage(named: "dish")
            cell.dateLabel.text = "2 mins ago"
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            return cell

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("feastDetail", sender: tableView)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "feastDetail" {
            let candyDetailViewController = segue.destinationViewController as! UIViewController

        }
    }

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        filteredFeasts.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "category CONTAINS[c] %@", searchController.searchBar.text)
        //let array = (feastEntries as NSMutableArray).filteredArrayUsingPredicate(searchPredicate)
    /*    let array = (feastEntries as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredFeasts = array as! [String]
        */
        self.tableView.reloadData()
    }

}

struct FeastEntry {
    let category : String
    let name : String
}

我有两个要求。第一个是如何根据 updateSearchResultsForSearchController 中的类别过滤 feastEntries 数组。 其次是如何为这种情况实现范围栏过滤器。

最佳答案

在 swift 中检查过滤方法...使用它你可以过滤任何集合类型,你可以这样写:

 let filteredEntry = self.feastEntries.filter { $0.0 == “Chocolate”//filter condition  }

ScopeBar 实现:

创建一个带有chocolate,hard等segment的分段控件... 根据所选段为该分段控件创建一个 IBaction,如上所示在 feastEntries 数组上应用过滤器。

关于ios - 使用 UISearchController 时如何过滤 updateSearchResultsForSearchController 中的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244530/

相关文章:

ios - Xcode 9.4 : Error generating code coverage

swift - 快速 append 到来自不同类的数组

ios - 如何使用 objective-c 在ios中使用Web服务URL实现搜索选项

swift - UISearchController 中的取消按钮在 iOS 13 中没有正确消失

ios - 带有 Segue 的 UISearchbar (UISearchResultsUpdating) 不会关闭

ios - 如何解决 Swift 4 URLSession.downloadTask 中的内存循环问题?

ios - Swift 请求用户访问照片库的权限

ios - 错误套接字 SO_NOAPNFALLBK 失败 : [42] Protocol not available, 转储回溯

ios - 我可以根据分段控制索引通过按钮快速移动屏幕吗?

objective-c - 如何根据 iOS 上的 touchMove 事件在屏幕上绘制动态矩形