ios – 在Swift中智能搜索Parse用户名不起作用

ios – 在Swift中智能搜索Parse用户名不起作用,第1张

概述我正在尝试在我的iOS应用程序中进行智能搜索,以便当用户在UISearchBar中键入字符时,结果会自动在搜索栏下方的tableview中更新.出于某种原因,当我在搜索栏中键入一个字符时,不会调用带有textDidChange的searchBar函数.在输入2个字符后调用它.所以我的搜索结果总是落后于实际输入搜索栏的一步.而且似乎每次都会调用search()函数两次.有任何想法吗? //FUNC: 我正在尝试在我的iOS应用程序中进行智能搜索,以便当用户在UISearchbar中键入字符时,结果会自动在搜索栏下方的tablevIEw中更新.出于某种原因,当我在搜索栏中键入一个字符时,不会调用带有textDIDChange的searchbar函数.在输入2个字符后调用它.所以我的搜索结果总是落后于实际输入搜索栏的一步.而且似乎每次都会调用search()函数两次.有任何想法吗?

//FUNC: searchfunc search(searchText: String? = nil){        if searchText == nil || searchText == "" {            println("No users found.")        } else {            var query = PFUser.query()            //MAKE CASE INSENSTIVE            query!.whereKey("username",containsstring: searchText!)            query!.findobjectsInBackgrounDWithBlock { (results,error) -> VoID in                if error != nil {                    println(error)                } else {                    if let res = results {                        self.data = res as? [PFUser]                    }                }            }        }        self.tableVIEw.reloadData()}overrIDe func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell {    let cell = self.tableVIEw.dequeueReusableCellWithIDentifIEr("Cell",forIndexPath: indexPath) as! UItableVIEwCell    let user = data[indexPath.row] as! PFUser    let username = user.username    println(username)    cell.textLabel?.text = username    return cell}func searchbar(searchbar: UISearchbar,textDIDChange searchText: String) {    search(searchText: searchText)    searchActive = false;}
@R_419_6120@ 您将需要实现UISearchResultsUpdating协议来实现此目的.它使用UISearchController(在iOS 8中引入),它必须以编程方式而不是通过故事板添加,但不要担心,它非常简单.

这应该为你完成工作

干杯,
罗素

class YourtableVIEwController: UItableVIEwController,UISearchbarDelegate,UISearchResultsUpdating {    var searchUsers: [PFUser] = [PFUser]()    var userSearchController = UISearchController()    var searchActive: Bool = false    // MARK: - lifecycle    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        self.userSearchController = UISearchController(searchResultsController: nil)        self.userSearchController.dimsBackgroundDuringPresentation = true        // This is used for dynamic search results updating while the user types        // Requires UISearchResultsUpdating delegate        self.userSearchController.searchResultsUpdater = self        // Configure the search controller's search bar        self.userSearchController.searchbar.placeholder = "Search for a user"        self.userSearchController.searchbar.sizetoFit()        self.userSearchController.searchbar.delegate = self        self.definesPresentationContext = true        // Set the search controller to the header of the table        self.tableVIEw.tableheaderVIEw = self.userSearchController.searchbar    }    // MARK: - Parse Backend methods    func loadSearchUsers(searchString: String) {        var query = PFUser.query()        // Filter by search string        query.whereKey("username",containsstring: searchString)        self.searchActive = true        query.findobjectsInBackgrounDWithBlock { (objects: [AnyObject]?,error: NSError?) -> VoID in            if (error == nil) {                self.searchUsers.removeAll(keepCapacity: false)                self.searchUsers += objects as! [PFUser]                self.tableVIEw.reloadData()            } else {                // Log details of the failure                println("search query error: \(error) \(error!.userInfo!)")            }            self.searchActive = false        }    }    // MARK: - Search bar Delegate Methods    func searchbarSearchbuttonClicked(searchbar: UISearchbar) {        // Force search if user pushes button        let searchString: String = searchbar.text.lowercaseString        if (searchString != "") {            loadSearchUsers(searchString)        }    }    func searchbarCancelbuttonClicked(searchbar: UISearchbar) {        // Clear any search criteria        searchbar.text = ""        // Force reload of table data from normal data source    }    // MARK: - UISearchResultsUpdating Methods    // This function is used along with UISearchResultsUpdating for dynamic search results processing    // Called anytime the search bar text is changed    func updateSearchResultsForSearchController(searchController: UISearchController) {        let searchString: String = searchController.searchbar.text.lowercaseString        if (searchString != "" && !self.searchActive) {            loadSearchUsers(searchString)        }    }    // MARK: - table vIEw data source    overrIDe func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int {        return 1    }    overrIDe func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        if (self.userSearchController.active) {            return self.searchUsers.count        } else {            // return whatever your normal data source is        }    }    overrIDe func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell {        var cell = tableVIEw.dequeueReusableCellWithIDentifIEr("userCell") as! UserCell        if (self.userSearchController.active && self.searchUsers.count > indexPath.row) {            // bind data to the search results cell        } else {            // bind data from your normal data source        }        return cell    }    // MARK: - UItableVIEwDelegate    overrIDe func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) {        tableVIEw.deselectRowAtIndexPath(indexPath,animated: true)        if (self.userSearchController.active && self.searchUsers.count > 0) {            // Segue or whatever you want        } else {            // normal data source selection        }    }}
总结

以上是内存溢出为你收集整理的ios – 在Swift中智能搜索Parse用户名不起作用全部内容,希望文章能够帮你解决ios – 在Swift中智能搜索Parse用户名不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1019781.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存