//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用户名不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)