gpt4 book ai didi

ios - 使搜索功能快速响应 ui 搜索栏中输入的文本 [swift]

转载 作者:行者123 更新时间:2023-11-30 13:34:23 27 4
gpt4 key购买 nike

我一直在开发我的 iOS 应用程序的搜索功能。目前我设法让它工作,但这样的方式是,只有当用户写完文本并单击搜索按钮时,搜索才会开始。即使输入第一个字母,如何通过查询 start 使其快速响应?我目前正在将搜索栏与 UIView Controller 一起使用,而不是 UITableViewController。

这是我的代码:

//
// 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)")


}


}

最佳答案

您正在 searchBarTextDidEndEditing 上触发搜索。相反,请使用 searchBar:textDidChange:,每次用户添加或删除字符时都会调用它。

(但是,如果您在这里使用 UISearchController 确实会更好。毕竟,这正是它的用途。)

关于ios - 使搜索功能快速响应 ui 搜索栏中输入的文本 [swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36243580/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com