简体   繁体   English

Parse.com查询表视图中的所有用户

[英]Parse.com querying all users in a tableview

How would I go about displaying a list of all users in the parse.com database in a tableview and then when they click on each table, display that particular user's information. 我将如何在tableview中显示parse.com数据库中所有用户的列表,然后在他们单击每个表时显示该特定用户的信息。

All I know is that in order to query the users I must use: 我所知道的是,要查询用户,我必须使用:

PFQuery *query = [PFUser query];

Thank you in advance and any help is much appreciated. 在此先感谢您,我们将不胜感激。

You're really asking a specific question about Parse queries, but it seems like you don't understand queries in general, so just start with a general query against Parse: 您实际上是在问一个有关Parse查询的特定问题,但似乎您不理解一般的查询,所以就从针对Parse的一般查询开始:

PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        // Something went wrong
    } else {
        // objects is an array of PFObject containing your results
    }
}];

As you figured out, you can also do a user query by making that first line: 如您所知,您还可以通过第一行进行用户查询:

PFQuery *query = [PFUser query];

The rest is the same. 其余部分相同。

Lyndsey Scott is right, this is basic stuff in the docs. Lyndsey Scott是对的,这是文档中的基本内容。 I'm posting this here because of the one "gotcha" which is the class name (_User instead of User) if you use the first method. 我将其发布在这里是因为,如果您使用第一种方法,则是一个“陷阱”,即类名(_User而不是User)。

What you generally will do is call [myTableView reloadData] inside the success block since you now have an array of users. 通常,您将要执行的操作是在成功块内调用[myTableView reloadData],因为您现在拥有一组用户。 In your didSelectCellAtIndexPath: perform a seque with a new viewcontroller, and in your prepareForSegue method, pass the user object to your pushed view controller so it knows what user to show. didSelectCellAtIndexPath:使用新的viewcontroller进行排序,然后在prepareForSegue方法中,将用户对象传递给推送的视图控制器,以便知道要显示的用户。

I assume you know how to use table view. 我假设您知道如何使用表格视图。 So I'll implement it this way: 因此,我将以这种方式实现它:

1.create a property 1.创建一个属性

@property (nonatomic) NSArray *users;

2.In viewDidAppear execute the query: 2.在viewDidAppear中执行查询:

PFQuery *query = [PFUser query];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        self.users = objects;
        [self.tableView reloadData];
    }
}];

3.You need to implement table view code, the most important is cell 3.您需要实现表格视图代码,最重要的是单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"userCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    PFUser *user = self.users[indexPath.row];
    cell.textLabel.text = user.username;
    return cell;
}

Beside that you also need all of the required table view's data source code to tell it the number of cell. 除此之外,您还需要所有必需的表视图的数据源代码来告诉它单元格的数量。 If you'll need some help with that just tell. 如果您需要帮助,请告诉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM