ios - 搜索栏不响应 iOS

标签 ios objective-c parse-platform uisearchbar pfquery

我对 iOS 开发和使用 treehouse 构建自毁 iOS 应用程序非常陌生,我们使用 parse.com 作为后端。

我已向应用程序添加了搜索栏:-

- (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

            }

        }];

    }
}

当我们搜索用户时,我们必须完全正确地获取用户名,包括完全正确地获取大写/小写字母,然后按搜索要显示的用户。

我希望即使我们没有得到正确的大写/小写字母,用户也能显示,所以进行不区分大小写的搜索。此外,如果用户没有获得正确的用户名,我们可以为接近该用户名的用户提供服务。

最佳答案

您应该将用户名的全小写值作为您的 PFUser 类或您尝试通过用户名查询的任何类的键。将搜索词添加到 PFQuery 时,确保它也全部小写。

您可以像这样将任何字符串转换为小写字符串:

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

所以当你创建用户时,你会做这样的事情:

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

然后当你想查询这个用户时你的查询看起来像

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

关于ios - 搜索栏不响应 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786249/

相关文章:

ios - 为什么我的代码不移动图像位置?

ios - 如何在 TableView 中保存数据?

objective-c - 如何将 NSTextView 添加到 NSScrollView 中?

ios - 在 Parse Cloud 或 healthkit 中存储 StepCount 的推荐方法是什么?

java - 如何在 onCreate 中使用从 parse 方法获取的变量

objective-c - iPhone : disable multi touch for App

ios - 如何在应用首次加载时运行函数

objective-c - 为什么我似乎必须在 iOS 应用程序委托(delegate)中强制转换 window.rootViewController?

ios - 像在 Facebook iOS 7 应用程序中一样获得漂亮的深色 UIToolbar 模糊

Android 5.1 推送通知图标为空白