2015-08-03 54 views
0

我對iOS開發非常陌生,並且正在用treehouse構建一個自毀的iOS應用程序,我們使用parse.com作爲後端。搜索欄無法響應iOS

我添加了一個搜索欄應用程序: -

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 

    //dismiss keyboard and reload table 
    [self.searchBar resignFirstResponder]; 
    [self.tableView reloadData]; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 

    //Enable the cancel button when the user touches the search field 
    self.searchBar.showsCancelButton = TRUE; 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { 

    //disable the cancel button when the user ends editing 
    self.searchBar.showsCancelButton = FALSE; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 

    //dismiss keyboard 
    [self.searchBar resignFirstResponder]; 

    //reset the foundUser property 
    self.foundUser = nil; 

    //Strip the whitespace off the end of the search text 
    NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 


    //Check to make sure the field isnt empty and Query parse for username in the text field 
    if (![searchText isEqualToString:@""]) { 

     PFQuery *query = [PFUser query]; 
     [query whereKey:@"username" equalTo:searchText]; 
     [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
      if (!error) { 

       //check to make sure the query actually found a user 
       if (objects.count > 0) { 

        //set your foundUser property to the user that was found by the query (we use last object since its an array) 
        self.foundUser = objects.lastObject; 

        //The query was succesful but returned no results. A user was not found, display error message 
       } else { 

       } 

       //reload the tableView after the user searches 
       [self.tableView reloadData]; 

      } else { 

       //error occurred with query 

      } 

     }]; 

    } 
} 

當我們搜索用戶,我們必須得到用戶名完全正確,包括越來越大/小寫字母完全正確再按搜索用戶顯示。

我希望用戶能夠顯示即使我們沒有得到大寫/小寫字母,因此做一個不區分大小寫的搜索。如果用戶沒有正確獲取用戶名,我們也可以爲用戶提供接近該用戶名的用戶名。

+0

檢查此鏈接。 http://stackoverflow.com/questions/31339298/how-to-search-array-of-dictionary-and-show-in-uitableview。我希望這個鏈接是有用的。 –

+0

有點。謝謝,但我不知道如何將它集成到parse.com – smithyy

回答

1

您應該將用戶名的全部小寫值保存爲PFUser類的密鑰,或者您嘗試通過用戶名查詢的任何類。將搜索項添加到PFQuery時,確保它全部小寫。

您可以將任何字符串變成小寫串這樣的:

NSString* string = @"AAA"; 
NSString* lowerCaseString = string.lowercaseString; 

所以,當你創建用戶你會做這樣的事情:

PFUser* user = [PFUser user]; 
user.username = self.usernameTextField.lowercaseString 
... 

然後當你想要查詢此用戶,您的查詢將看起來像

... 
[query whereKey:@"username" containsString:searchString.lowercaseString]; 
... 
+0

用戶,但在搜索欄中,當您鍵入第一個字母時,它會自動變爲大寫,直到您取消選擇鍵盤上的shift按鈕,是不是有我可以添加,以便我可以鍵入用戶名大寫或小寫用戶被發現 – smithyy

+0

檢查我的編輯,看看如何將字符串轉換成小寫字符串 –

+0

將嘗試它,但是有沒有辦法,我們可以在大小寫搜索所以它可以顯示用戶 – smithyy