3

我目前通过 parse.com 设置了我的系统

我试图做的是能够在一个tableView或一个PFQuerTableViewController能够查询PFObject我的用户发布的帖子。目前我在下面有这段代码,它只说“正在加载..”而不是显示他们的帖子。为什么它不显示我用户的最新帖子?

这是我试图获取用户帖子的查询:

- (PFQuery *)queryForTable {

    PFQuery *postQuery = [PFQuery queryWithClassName:@"New"];
    [postQuery whereKey:@"author" equalTo:[PFUser currentUser]];

    // Run the query
    [postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            //Save results and update the table
            postArray = objects;
            [self.tableView reloadData];
        }
    }];
}

这是它的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    // If you want a custom cell, create a new subclass of PFTableViewCell, set the cell identifier in IB, then change this string to match
    // You can access any IBOutlets declared in the .h file from here and set the values accordingly
    // "Cell" is the default cell identifier in a new Master-Detail Project
    static NSString *CellIdentifier = @"Cell";

    PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell
    //cell.textLabel.text = [object objectForKey:self.textKey];
    //cell.imageView.file = [object objectForKey:self.imageKey];
    PFObject *post = [postArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:[post objectForKey:@"title"]];

    return cell;
}

这里也是我如何发布 PFObject 的代码:

-(IBAction)done:(id)sender {

    PFUser *user = [PFUser currentUser];

    PFObject *quoteNew = [PFObject objectWithClassName:@"New"];
    [quoteNew setObject:user forKey:@"user"];
    [quoteNew setObject:[[self quoteText] text] forKey:@"quoteText"];
    [quoteNew setObject:[[self attributionTitle] text] forKey:@"title"];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Uploading";
    [hud show:YES];

    [quoteNew saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            //[self done:self];
            [hud hide:YES];

            [self dismissViewControllerAnimated:YES completion:nil];

        } else {

             [hud hide:YES];

        }

    }];
4

2 回答 2

0

您正在将密钥设置为用户

[quoteNew setObject:user forKey:@"user"];

然后查询作者

[postQuery whereKey:@"author" equalTo:[PFUser currentUser]];

试试吧

[postQuery whereKey:@"user" equalTo:[PFUser currentUser]];
于 2014-03-11T01:36:11.737 回答
0

您正在尝试在queryForTable方法中运行查询。那不是它的用途。在queryForTable中,您应该使用您想要的约束(例如您添加的作者)构建查询,然后返回查询。所以对于你的情况:

- (PFQuery *)queryForTable {

    PFQuery *postQuery = [PFQuery queryWithClassName:@"New"];
    [postQuery whereKey:@"author" equalTo:[PFUser currentUser]];

    // Don't run the query, return it so PFQueryTableViewcontroller can use it
    return postQuery;
}

如果您想以编程方式告诉您的 PFQueryTableViewController 加载新帖子,您可以运行[self loadObjects].

注意:这仅适用于子类化 PFQueryTableViewController

我建议阅读文档,它在这里很好地列出了所有内容。

于 2013-11-11T06:20:22.773 回答