【问题标题】:search bar not responsive iOS搜索栏没有响应 iOS
【发布时间】:2015-08-03 11:38:15
【问题描述】:

我对 iOS 开发和使用树屋构建自毁 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

            }

        }];

    }
}

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

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

【问题讨论】:

标签: ios objective-c parse-platform uisearchbar pfquery


【解决方案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];
...

【讨论】:

  • 好的,但是在搜索栏中,当您键入第一个字母时,它会自动变为大写字母,直到您取消选择键盘上的 shift 按钮,我是否可以添加一些内容以便我可以键入用户名大写或小写的用户被找到
  • 检查我的编辑以了解如何将字符串转换为小写字符串
  • 会试一试,但我们没有办法以大写/小写形式搜索,以便向用户显示
  • 他们不提供不区分大小写的搜索
猜你喜欢
  • 2012-09-23
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多