我在iOS中实现了一个搜索栏,我必须输入用户返回的确切用户名。我如何改变它,以便它返回类似于我搜索的用户。我使用parse.com作为后端。
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 {
}
}];
in cellforRow:
PFUser *user = self.foundUser;
if (self.foundUser) {
//set the label as the foundUsers username
cell.textLabel.text = self.foundUser.username;
如何让它变得更好,以便显示类似的用户或用户输入的字母?
答案 0 :(得分:1)
您是否尝试过Parse的PFQuery
文档?看起来PFQuery
有whereKey:ContainsString:
方法,可以满足您的需求。
https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/whereKey:containsString: