{"id":599,"date":"2023-02-25T20:04:57","date_gmt":"2023-02-25T12:04:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=599"},"modified":"2023-02-26T07:55:51","modified_gmt":"2023-02-25T23:55:51","slug":"swift-ui-swift-ui-search-bar-uisearchbar","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-swift-ui-search-bar-uisearchbar\/","title":{"rendered":"Swift UI &#8211; \u641c\u7d22\u6761\uff08UISearchBar\uff09\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>\u641c\u7d22\u6761\u6837\u5f0f<\/h3>\n<p>\u641c\u7d22\u6761Options\u5c5e\u6027\u53ef\u8bbe\u7f6e\u5982\u4e0b\u529f\u80fd\u6837\u5f0f\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>Shows Search Results Button\uff1a\u52fe\u9009\u540e\uff0c\u641c\u7d22\u6846\u53f3\u8fb9\u663e\u793a\u4e00\u4e2a\u5706\u5f62\u5411\u4e0b\u7684\u6309\u94ae\uff0c\u5355\u51fb\u4f1a\u53d1\u9001\u7279\u6b8a\u4e8b\u4ef6\u3002<\/li>\n<li>Shows Bookmarks Button\uff1a\u52fe\u9009\u540e\uff0c\u641c\u7d22\u6846\u53f3\u8fb9\u4f1a\u663e\u793a\u4e00\u4e2a\u4e66\u672c\u7684\u6309\u94ae\uff0c\u5355\u51fb\u4f1a\u53d1\u9001\u7279\u6b8a\u4e8b\u4ef6\u3002<\/li>\n<li>Shows Cancel Button\uff1a\u52fe\u9009\u540e\uff0c\u641c\u7d22\u6846\u53f3\u8fb9\u4f1a\u51fa\u73b0\u4e00\u4e2a\u201cCancel\u201d\u6309\u94ae\uff0c\u5355\u51fb\u4f1a\u53d1\u9001\u7279\u6b8a\u4e8b\u4ef6\u3002<\/li>\n<li>Shows Scope Bar\uff1a\u52fe\u9009\u540e\uff0c\u4f1a\u5728\u641c\u7d22\u6761\u4e0b\u9762\u51fa\u73b0\u4e00\u4e2a\u5206\u6bb5\u63a7\u5236\u5668\u3002<\/li>\n<\/ul>\n<h3>\u4f7f\u7528\u6848\u4f8b<\/h3>\n<p>\u529f\u80fd\u9700\u6c42\uff1a<\/p>\n<p>\uff081\uff09\u5728Main.storyboard\u754c\u9762\u91cc\u62d6\u5165\u4e00\u4e2a<code>SearchBar<\/code>\u548c\u4e00\u4e2a<code>TableView<\/code>\uff0c<code>SearchBar<\/code>\u653e\u5230<code>TableView<\/code>\u7684\u9875\u7709\u4f4d\u7f6e<br \/>\n\uff082\uff09\u521d\u59cb\u5316\u6216\u8005\u641c\u7d22\u6761\u4e3a\u7a7a\u65f6\uff0c\u8868\u683c\u663e\u793a\u6240\u6709\u6570\u636e<br \/>\n\uff083\uff09\u641c\u7d22\u6761\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u8868\u683c\u5b9e\u65f6\u8fc7\u6ee4\u663e\u793a\u5339\u914d\u7684\u9879\u76ee<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController, UISearchBarDelegate, \n        UITableViewDataSource, UITableViewDelegate {\n\n    \/\/ \u5f15\u7528\u901a\u8fc7storyboard\u521b\u5efa\u7684\u63a7\u4ef6\n    @IBOutlet var searchBar : UISearchBar!\n    @IBOutlet var tableView : UITableView!\n\n    \/\/ \u6240\u6709\u7ec4\u4ef6\n    var ctrls:[String] = [&quot;Label&quot;,&quot;Button1&quot;,&quot;Button2&quot;,&quot;Switch&quot;]\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        \/\/\u8bbe\u7f6e\u4ee3\u7406\n        self.searchBar.delegate = self\n        self.tableView.delegate = self\n        self.tableView.dataSource = self\n        \/\/ \u6ce8\u518cTableViewCell\n        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: &quot;cell&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, cellForRowAt indexPath: IndexPath)\n        -&gt; UITableViewCell {\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;cell&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 = tableView.dequeueReusableCell(withIdentifier: identify,\n                                                for: indexPath)\n        cell.accessoryType = .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        print(searchText)\n        \/\/ \u6ca1\u6709\u641c\u7d22\u5185\u5bb9\u65f6\u663e\u793a\u5168\u90e8\u7ec4\u4ef6\n        if searchText == &quot;&quot; {\n            self.ctrlsel = self.ctrls\n        }\n        else { \/\/ \u5339\u914d\u7528\u6237\u8f93\u5165\u5185\u5bb9\u7684\u524d\u7f00(\u4e0d\u533a\u5206\u5927\u5c0f\u5199)\n            self.ctrlsel = []\n            for ctrl in self.ctrls {\n                if ctrl.lowercased().hasPrefix(searchText.lowercased()) {\n                    self.ctrlsel.append(ctrl)\n                }\n            }\n        }\n        \/\/ \u5237\u65b0Table View\u663e\u793a\n        self.tableView.reloadData()\n    }\n\n    \/\/ \u641c\u7d22\u4ee3\u7406UISearchBarDelegate\u65b9\u6cd5\uff0c\u70b9\u51fb\u865a\u62df\u952e\u76d8\u4e0a\u7684Search\u6309\u94ae\u65f6\u89e6\u53d1\n    \/**\n    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {\n        searchBar.resignFirstResponder()\n    }\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 \u641c\u7d22\u6761\u6837\u5f0f \u641c\u7d22\u6761Options\u5c5e\u6027\u53ef\u8bbe\u7f6e\u5982\u4e0b\u529f\u80fd\u6837\u5f0f\uff1a Shows Searc [&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-599","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/599","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=599"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}