0
私は検索機能のためにiosアプリケーションを開発しています。現在、私はそれを動作させることができましたが、ユーザーがテキストを書いて検索ボタンをクリックするだけで検索が開始されるようになりました。最初の文字が入力されている場合でも、開始を照会することによって、それを迅速に応答させることはできますか?現在、UITableViewControllerではなく、UIViewコントローラで検索バーを使用しています。検索機能をui検索バーのキー入力されたテキストにすばやく応答させます。
//
// CoffeeListViewController.swift
// CoffeeApp
//
// Created by izzuddin on 14/03/2016.
// Copyright © 2016 izzuddin. All rights reserved.
//
import UIKit
import CloudKit
import FBSDKCoreKit
import FBSDKLoginKit
class CoffeeListViewController: UIViewController {
////////////////////////////OUTLET
@IBOutlet weak var tableview: UITableView!
var coffees = [Coffee]()
var filteredNames = [Coffee]()
@IBOutlet weak var searchBar: UISearchBar!
////////////////////////////SPINNER
var loadingView = UIView()
var container = UIView()
var activityIndicator = UIActivityIndicatorView()
func showLoading() {
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
self.loadingView = UIView(frame: win.frame)
self.loadingView.tag = 1
self.loadingView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0)
win.addSubview(self.loadingView)
container = UIView(frame: CGRect(x: 0, y: 0, width: win.frame.width/3, height: win.frame.width/3))
container.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.6)
container.layer.cornerRadius = 10.0
container.layer.borderColor = UIColor.grayColor().CGColor
container.layer.borderWidth = 0.5
container.clipsToBounds = true
container.center = self.loadingView.center
activityIndicator.frame = CGRectMake(0, 0, win.frame.width/5, win.frame.width/5)
activityIndicator.activityIndicatorViewStyle = .WhiteLarge
activityIndicator.center = self.loadingView.center
self.loadingView.addSubview(container)
self.loadingView.addSubview(activityIndicator)
activityIndicator.startAnimating()
}
func hideLoading(){
UIView.animateWithDuration(0.0, delay: 1.0, options: .CurveEaseOut, animations: {
self.container.alpha = 0.0
self.loadingView.alpha = 0.0
self.activityIndicator.stopAnimating()
}, completion: { finished in
self.activityIndicator.removeFromSuperview()
self.container.removeFromSuperview()
self.loadingView.removeFromSuperview()
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
let removeView = win.viewWithTag(1)
removeView?.removeFromSuperview()
})
}
////////////////////////////LOAD DATA
func call_data(){
self.showLoading()
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let query = CKQuery(recordType: "Coffee", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
publicData.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
for coffee in results! {
let newCoffee = Coffee()
newCoffee.name = coffee["Name"] as! String
newCoffee.cafe = coffee["Cafe"] as! String
newCoffee.rating = coffee["Rating"] as! Double
newCoffee.place = coffee["Place"] as? CLLocation
self.coffees.append(newCoffee)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.tableview.reloadData()
self.hideLoading()
})
}
}
else {
print(error)
}
}
}
//////////////////////////////////////////////////VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
call_data()
}
//////////////////////////////////////////////////PREPARE SEGUE
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "AddCoffeeVC" {
let destVC: AddCoffeeViewController = segue.destinationViewController as! AddCoffeeViewController
destVC.delegate = self
}
if segue.identifier == "showCoffeeVC" {
let destVC: ShowCoffeeViewController = segue.destinationViewController as! ShowCoffeeViewController
if let selectedIndexPath = self.tableview.indexPathForSelectedRow {
let coffee: Coffee = self.coffees[selectedIndexPath.row]
destVC.coffeeDetail = coffee
}
}
}
}
////////////////////////////PASSING BACK DATA
extension CoffeeListViewController : AddCoffeeDelegate{
func viewController(vc: AddCoffeeViewController, didAddCoffee coffee: Coffee!) {
self.coffees.append(coffee)
//create the cloudkit record here
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase
let record = CKRecord(recordType: "Coffee")
record.setValue(coffee.name, forKey: "Name")
record.setValue(coffee.cafe, forKey: "Cafe")
record.setValue(coffee.rating, forKey: "Rating")
record.setValue(coffee.place, forKey: "Place")
publicData.saveRecord(record, completionHandler: { record, error in
if error != nil {
print(error)
}
})
self.dismissViewControllerAnimated(true, completion: nil)
self.tableview.reloadData()
}
}
////////////////////////////////////////////////// ADPOPT TABLE VIEW
extension CoffeeListViewController : UITableViewDelegate, UITableViewDataSource{
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.coffees.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("CoffeeCell", forIndexPath: indexPath)
let coffee = coffees[indexPath.row]
cell.textLabel!.text = coffee.name
cell.detailTextLabel!.text = "\(coffee.rating)"
return cell
}
}
////////////////////////////////////////////////// SEARCH BAR
extension CoffeeListViewController : UISearchBarDelegate {
func searchBarShouldEndEditing(searchBar: UISearchBar)-> Bool{
searchBar.showsCancelButton = true
return searchBar.showsCancelButton.boolValue
}
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool{
searchBar.showsCancelButton = true
return searchBar.showsCancelButton.boolValue
}
func searchBarCancelButtonClicked(searchBar: UISearchBar){
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: false)
call_data()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar){
searchBar.resignFirstResponder()
}
func searchBarTextDidEndEditing(searchBar: UISearchBar){
var empty_array: [Coffee] = []
for coffee in coffees{
if searchBar.text?.lowercaseString == coffee.name {
empty_array.append(coffee)
}
}
self.coffees = empty_array
self.tableview.reloadData()
NSLog("\(searchBar.text)")
}
}