【问题标题】:How to query the user's post?如何查询用户的帖子?
【发布时间】:2013-11-11 01:31:40
【问题描述】:

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

我想要做的是能够在tableViewPFQuerTableViewController 中查询我的用户发布的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];

        }

    }];

【问题讨论】:

    标签: ios parse-platform


    【解决方案1】:

    您正尝试在 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

    我建议阅读文档,它对所有内容都进行了很好的安排here

    【讨论】:

    • 我在发布 PFObject 时会添加“作者”吗?我会更改下面的代码以添加“作者”吗? 'PFObject *quoteNew = [PFObject objectWithClassName:@"New"]; [quoteNew setObject:user forKey:@"user"]; [quoteNew setObject:[[self quoteText] text] forKey:@"quoteText"]; [quoteNew setObject:[[self attributionTitle] text] forKey:@"title"];'
    • 您何时尝试创建新对象?在您的问题中,您将它放在 IBOutlet 中 - 您想在用户点击某物时运行它吗?
    • 是的,当我创建对象时,我必须合并“作者”吗?
    【解决方案2】:

    您将密钥设置为用户

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

    然后查询作者

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

    试试吧

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

    【讨论】:

      猜你喜欢
      • 2014-02-01
      • 2020-12-28
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2018-08-16
      • 2015-10-23
      相关资源
      最近更新 更多