swift
1import UIKit 2 3class EditViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate { 4 5 @IBOutlet weak var EditViewTabelView:UITableView! 6 @IBOutlet weak var AddBtr: UIButton! 7 @IBOutlet weak var searchBar: UISearchBar! 8 var searchResults:[String] = [] 9 10 11 12 class segue{ 13 var data = [String]() 14 } 15 16 let key = segue() 17 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 22 EditViewTabelView.tableFooterView = UIView(frame: .zero) 23 EditViewTabelView.frame = CGRect(x: 0, y: 100, width: 375, height: 550) 24 AddBtr.layer.cornerRadius = 30 //角のR設定 25 AddBtr.layer.masksToBounds = true 26 self.view.backgroundColor = UIColor.rgb(r: 204, g: 250, b: 125, alpha: 1) 27 28 searchBar.placeholder = "検索したいものを入力してください" 29 searchBar.delegate = self 30 searchBar.setValue("キャンセル", forKey: "_cancelButtonText") 31 searchBar.tintColor = UIColor.red 32 searchBar.frame = CGRect(x: 0, y: 64, width: 375, height: 40) 33 34 } 35 36 @IBAction func AddBtr(_ sender: Any) { 37 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 38 39 // OKボタンの設定 40 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 41 (action:UIAlertAction!) -> Void in 42 43 // OKを押した時入力されていたテキストを表示 44 if let textFields = alert.textFields { 45 46 // アラートに含まれるすべてのテキストフィールドを調べる 47 for textField in textFields { 48 self.key.data.append(textField.text!) 49 self.EditViewTabelView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 50 //self.EditViewTabelView?.insertRows(at: [IndexPath(row: 0, section: 0)], with: .top) 51 print(textField.text!) 52 } 53 self.EditViewTabelView.reloadData() 54 } 55 }) 56 alert.addAction(okAction) 57 58 // キャンセルボタンの設定 59 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 60 alert.addAction(cancelAction) 61 62 // テキストフィールドを追加 63 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 64 textField.placeholder = "テキスト" 65 }) 66 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 67 68 self.present(alert, animated: true, completion: nil) 69 } 70 71 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 72 if searchBar.text != "" { 73 return searchResults.count 74 } else { 75 return key.data.count 76 } 77 78 } 79 80 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 81 let Cell = tableView.dequeueReusableCell(withIdentifier: "celltext", for: indexPath) 82 83 if searchBar.text != "" { 84 Cell.textLabel!.text = "(searchResults[indexPath.row])" 85 } else { 86 Cell.textLabel?.text = key.data[indexPath.row] 87 } 88 89 return Cell 90 } 91 92 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 93 self.view.endEditing(true) 94 searchBar.showsCancelButton = true 95 self.searchResults = key.data.filter{ 96 $0.lowercased().contains(searchBar.text!.lowercased()) 97 } 98 self.EditViewTabelView.reloadData() 99 } 100 101 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 102 searchBar.showsCancelButton = false 103 self.view.endEditing(true) 104 searchBar.text = "" 105 self.EditViewTabelView.reloadData() 106 } 107 108 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 109 searchBar.showsCancelButton = true 110 return true 111 } 112 113}
実現したい事
サーチバーで検索するとき、入力中から絞り込みを出来るようにしたい
それとキャンセルボタンを押さなくても入力途中にサーチバー内の×をタップしたらキャンセル出来るようにしたい
出来ている事
基本的なサーチバーの実装まではなんとか出来た。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/17 15:23
退会済みユーザー
2018/06/24 11:54