{"id":1504,"date":"2018-01-03T14:31:27","date_gmt":"2018-01-03T05:31:27","guid":{"rendered":"http:\/\/blue-bear.jp\/kb\/?p=1504"},"modified":"2018-01-03T14:31:27","modified_gmt":"2018-01-03T05:31:27","slug":"swift4-tableview%e3%81%abuisearchbar%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/swift4-tableview%e3%81%abuisearchbar%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b\/","title":{"rendered":"[swift4] tableView\u306bUISearchBar\u3092\u5b9f\u88c5\u3059\u308b"},"content":{"rendered":"<p>tableView\u306bUISearchBar\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5<\/p>\n<pre class=\"lang:default decode:true \">class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {\r\n \r\n @IBOutlet weak var tableView: UITableView!\r\n var searchBar = UISearchBar()\r\n \r\n var posts:[String] = [\"Google\",\"Amazon\",\"Facebook\",\"Apple\"]\r\n\r\n \/\/MARK: didload\r\n override func viewDidLoad() {\r\n  super.viewDidLoad()\r\n \r\n  tableView.dataSource = self\r\n  tableView.delegate = self\r\n \r\n  searchBar = UISearchBar()\r\n  searchBar.delegate = self as! UISearchBarDelegate\r\n  searchBar.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:42)\r\n  searchBar.layer.position = CGPoint(x: self.view.bounds.width\/2, y: 89)\r\n  searchBar.searchBarStyle = UISearchBarStyle.default\r\n  searchBar.showsSearchResultsButton = false\r\n  searchBar.placeholder = \"\u691c\u7d22\"\r\n  searchBar.setValue(\"\u30ad\u30e3\u30f3\u30bb\u30eb\", forKey: \"_cancelButtonText\")\r\n  searchBar.tintColor = UIColor.red\r\n \r\n  tableView.tableHeaderView = searchBar\r\n \r\n }\r\n \r\n \r\n override func didReceiveMemoryWarning() {\r\n  super.didReceiveMemoryWarning()\r\n }\r\n \r\n \/\/MARK: TableView Method\r\n func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\r\n  return posts.count\r\n }\r\n\r\n \/\/MARK: cellSetting\r\n func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {\r\n  let cell = tableView.dequeueReusableCell(withIdentifier: \"cell\", for: indexPath)\r\n  return cell\r\n }\r\n \r\n \/\/MARK: SearchBarAction\r\n \/\/ \u691c\u7d22\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u6642\u306b\u547c\u3070\u308c\u308b\r\n func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {\r\n  print(\"searchBarSearchButtonClicked\")\r\n  self.view.endEditing(true)\r\n  searchBar.showsCancelButton = true\r\n  searchWord = searchBar.text!\r\n  self.searchResults = posts.filter{\r\n   $0.lowercased().contains(searchBar.text!.lowercased())\r\n  }\r\n  self.tableView.reloadData()\r\n }\r\n \r\n \/\/ \u30ad\u30e3\u30f3\u30bb\u30eb\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u6642\u306b\u547c\u3070\u308c\u308b\r\n func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {\r\n  print(\"searchBarCancelButtonClicked\")\r\n  searchBar.showsCancelButton = false\r\n  self.view.endEditing(true)\r\n  searchBar.text = \"\"\r\n  tableView.reloadData()\r\n }\r\n \r\n \/\/ \u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u5165\u529b\u958b\u59cb\u524d\u306b\u547c\u3070\u308c\u308b\r\n func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -&gt; Bool {\r\n  print(\"searchBarShouldBeginEditing\")\r\n  searchBar.showsCancelButton = true\r\n  return true\r\n }\r\n \r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>tableView\u306bUISearchBa<\/p>\n","protected":false},"author":1,"featured_media":88,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1504"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=1504"}],"version-history":[{"count":1,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1504\/revisions"}],"predecessor-version":[{"id":1505,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/1504\/revisions\/1505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media\/88"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=1504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=1504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=1504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}