{"id":600,"date":"2023-02-25T20:05:34","date_gmt":"2023-02-25T12:05:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=600"},"modified":"2023-02-26T07:54:22","modified_gmt":"2023-02-25T23:54:22","slug":"swift-ui-search-bar-with-result-list-uisearchdisplaycontroller","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-search-bar-with-result-list-uisearchdisplaycontroller\/","title":{"rendered":"Swift UI &#8211; \u5e26\u7ed3\u679c\u5217\u8868\u7684\u641c\u7d22\u6761\uff08UISearchDisplayController\uff09\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<br \/>\n\u6ce8\uff1a\u81eaiOS8\u8d77\uff0c\u82f9\u679c\u4fbf\u5e9f\u5f03UISearchDisplayController\u7684\u4f7f\u7528\uff0c\u6539\u4e3a\u4f7f\u7528UISearchController\u6765\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd<\/p>\n<\/blockquote>\n<p>UISearchDisplayController\u63a7\u4ef6\u9ed8\u8ba4\u5c01\u88c5\u4e86UISearchBar\u548cUITableView\uff0c\u53ef\u540c\u65f6\u63d0\u4f9b\u641c\u7d22\u548c\u7ed3\u679c\u8868\u683c\u663e\u793a\u529f\u80fd\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4ee5\u4e0b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f7f\u7528\u6837\u4f8b\uff0c\u540c\u65f6\u901a\u8fc7\u4ee3\u7801\u5b9a\u5236UISearchBar\u7684\u4e00\u4e9b\u5c5e\u6027\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u5916\u89c2\u548c\u6548\u679c\uff0c\u5e76\u4e14\u5c55\u793a\u7528\u4e8e\u9009\u62e9\u641c\u7d22\u8303\u56f4\u7684\u5206\u6bb5\u6761\u7684\u7528\u6cd5\u3002<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController, UISearchBarDelegate {\n\n    \/\/ \u5f15\u7528\u901a\u8fc7storyboard\u521b\u5efa\u7684\u63a7\u4ef6\n    @IBOutlet var searchDisplay: UISearchDisplayController!\n\n    \/\/ \u6240\u6709\u7ec4\u4ef6\n    var ctrls:[String] = [&quot;Label&quot;,&quot;Button1-\u521d\u7ea7&quot;,&quot;Button1-\u9ad8\u7ea7&quot;,&quot;Button2-\u521d\u7ea7&quot;,&quot;Button2-\u9ad8\u7ea7&quot;,&quot;Switch&quot;]\n\n    \/\/ \u641c\u7d22\u5339\u914d\u7684\u7ed3\u679c\uff0cTable View\u4f7f\u7528\u8fd9\u4e2a\u6570\u7ec4\u4f5c\u4e3adatasource\n    var ctrlsel:[String] = []\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        \/\/ \u8d77\u59cb\u52a0\u8f7d\u5168\u90e8\u5185\u5bb9\n        self.ctrlsel = self.ctrls\n        \/\/ \u6ce8\u518cTableViewCell\n        self.searchDisplay.searchResultsTableView.registerClass(UITableViewCell.self,\n            forCellReuseIdentifier: &quot;SwiftCell&quot;)\n        \/\/ \u8bbe\u7f6e\u6587\u672c\u63d0\u793a\n        self.searchDisplay.searchBar.placeholder = &quot;\u8f93\u5165\u641c\u7d22\u4fe1\u606f&quot;\n        \/\/ \u53ef\u4ee5\u8bbe\u7f6e\u521d\u59cb\u503c\n        \/\/ self.searchDisplay.searchBar.text = &quot;b&quot;\n        \/\/ \u8bbe\u7f6e\u641c\u7d22\u680f\u63d0\u793a\u4fe1\u606f\uff08\u6807\u9898\uff09\n        self.searchDisplay.searchBar.prompt = &quot;\u641c\u7d22\u7ec4\u4ef6\u540d\u79f0&quot;\n        \/\/ \u4e0d\u663e\u793aSearch Bar\u8fb9\u6846\n        self.searchDisplay.searchBar.searchBarStyle = UISearchBarStyle.Minimal\n        \/\/ \u663e\u793a\u5206\u6bb5\u6761\n        self.searchDisplay.searchBar.showsScopeBar = true\n        self.searchDisplay.searchBar.scopeButtonTitles = [&quot;\u5168\u90e8&quot;,&quot;\u521d\u7ea7&quot;,&quot;\u9ad8\u7ea7&quot;]\n    }\n\n    \/\/ \u8fd4\u56de\u8868\u683c\u884c\u6570\uff08\u4e5f\u5c31\u662f\u8fd4\u56de\u63a7\u4ef6\u6570\uff09\n    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -&gt; Int {\n        return self.ctrlsel.count\n    }\n\n    \/\/ \u521b\u5efa\u5404\u5355\u5143\u663e\u793a\u5185\u5bb9(\u521b\u5efa\u53c2\u6570indexPath\u6307\u5b9a\u7684\u5355\u5143\uff09\n    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!)\n        -&gt; UITableViewCell!\n    {\n        \/\/ \u4e3a\u4e86\u63d0\u4f9b\u8868\u683c\u663e\u793a\u6027\u80fd\uff0c\u5df2\u521b\u5efa\u5b8c\u6210\u7684\u5355\u5143\u9700\u91cd\u590d\u4f7f\u7528\n        let identify:String = &quot;SwiftCell&quot;\n        \/\/ \u540c\u4e00\u5f62\u5f0f\u7684\u5355\u5143\u683c\u91cd\u590d\u4f7f\u7528\uff0c\u5728\u58f0\u660e\u65f6\u5df2\u6ce8\u518c\n        let cell = self.searchDisplay.searchResultsTableView.dequeueReusableCellWithIdentifier(\n            identify, forIndexPath: indexPath) as UITableViewCell\n        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator\n        cell.textLabel?.text = self.ctrlsel[indexPath.row]\n        return cell\n    }\n\n    \/\/ \u641c\u7d22\u4ee3\u7406UISearchBarDelegate\u65b9\u6cd5\uff0c\u6bcf\u6b21\u6539\u53d8\u641c\u7d22\u5185\u5bb9\u65f6\u90fd\u4f1a\u8c03\u7528\n    func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {\n        self.searchText = searchText\n        searchCtrls()\n    }\n\n    \/\/ \u9009\u62e9\u5206\u6bb5\u6761\u65f6\u8c03\u7528\n    func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {\n        print(selectedScope)\n        searchCtrls();\n    }\n\n    \/\/ \u4fdd\u5b58\u641c\u7d22\u5185\u5bb9\n    var searchText:String = &quot;&quot;\n\n    \/\/ \u641c\u7d22\u8fc7\u6ee4\n    func searchCtrls() {\n        \/\/ \u6ca1\u6709\u641c\u7d22\u5185\u5bb9\u65f6\u663e\u793a\u5168\u90e8\u7ec4\u4ef6\n        if self.searchText == &quot;&quot; {\n            self.ctrlsel = self.ctrls\n        } else {\n            var scope = self.searchDisplay.searchBar.selectedScopeButtonIndex;\n            \/\/ \u5339\u914d\u7528\u6237\u8f93\u5165\u5185\u5bb9\u7684\u524d\u7f00\n            self.ctrlsel = []\n            for ctrl in self.ctrls {\n                let lc = ctrl.lowercaseString\n                if lc.hasPrefix(self.searchText) {\n                    if (scope == 0 || (scope == 1 &amp;&amp; lc.hasSuffix(&quot;\u521d\u7ea7&quot;))\n                        || (scope == 2 &amp;&amp; lc.hasSuffix(&quot;\u9ad8\u7ea7&quot;))) {\n                        self.ctrlsel.append(ctrl)\n                    }\n                }\n            }\n        }\n\n        \/\/ \u4e0d\u9700\u8981\u5237\u65b0Table View\u663e\u793a\n        \/\/ self.searchDisplay.searchResultsTableView.reloadData()\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u6ce8\uff1a\u81eaiOS8\u8d77\uff0c\u82f9\u679c\u4fbf\u5e9f\u5f03UISearchDisplayController\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-600","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}