2015-08-03 63 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