我正在使用这个查询来查找用户,它是有效的,但它只显示了第一个用户。我想让它向我显示一个UITextField文本的用户。我该怎么做呢?(我有一个文本字段,在那里我输入一个名称,然后它应该显示解析后的用户和名称)
PFQuery *query = [PFUser query];
NSArray *users = [query findObjects];
userQuerys.text = users[0][@"username"];非常感谢
发布于 2015-06-12 02:29:23
此代码将获取其中username等于name参数的所有PFUser:
- (void)searchUsersNamed:(NSString *)name withCompletion:(void (^)(NSArray *users))completionBlock {
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:name];
[query findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
if (!error) {
// we found users with that username
// run the completion block with the users.
// making sure the completion block exists
if (completionBlock) {
completionBlock(users);
}
} else {
// log details of the failure
NSLog(@"Error: %@ %@", error, [error description]);
}
}];
}例如,如果您需要使用结果更新UI,例如,一个表:
- (void)someMethod {
// we will grab a weak reference of self to perform
// work inside the completion block
__weak ThisViewController *weakSelf = self;
//replace ThisViewController with the correct self class
[self searchUsersNamed:@"Phillipp" withCompletion:^(NSArray *users) {
//perform non-UI related logic here.
//set the found users inside the array used by the
//tableView datasource. again, just an example.
weakSelf.users = users;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//pefrorm any UI updates only
//for example, update a table
[weakSelf.tableView reloadData];
}];
}];
}注意:如果出现错误,这里的completionBlock将不会运行,但即使没有找到用户,它也会运行,因此您必须对此进行处理(如果需要。在这个例子中,它是不需要的)。
避免在那个mainQueue方法上运行与UI无关的逻辑,你可能会锁定主线程,这是糟糕的用户体验。
https://stackoverflow.com/questions/30788115
复制相似问题