显示用户名 - 解析

时间:2014-05-17 16:53:24

标签: ios objective-c parse-platform

我在互联网上看了一遍,一无所获,请帮助我。我想弄清楚如何在表格视图(detailTextLabel)中显示解析用户名。当我执行下面的代码时,我的应用只显示PFUser Id而不是用户名。它会显示:<PFUser:H2AhEbYGal:(null)> {,但我试图显示该帖子的用户名。

继承我保存对象:

    PFUser *user = [PFUser currentUser];

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

这是我尝试在PFQuery中检索用户:

 PFQuery *query = [PFQuery queryWithClassName:@"New"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded. The first 100 objects are available in objects'
            [query includeKey:@"author"];
           // PFObject *testObject = [query findObjects];
           // PFUser *testUser = [testObject objectForKey:@"author"];
           // NSLog(@"username: %@",testUser.username);

        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

这是我试图展示它:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@",[object objectForKey:@"author"],[dateFormat stringFromDate:updated]];

您可以在[object objectForKey:@"author"]代码中担心cell.detailTextLabel.text

1 个答案:

答案 0 :(得分:1)

此:

PFQuery *query = [PFQuery queryWithClassName:@"New"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded. The first 100 objects are available in objects'
        [query includeKey:@"author"];
       // PFObject *testObject = [query findObjects];
       // PFUser *testUser = [testObject objectForKey:@"author"];
       // NSLog(@"username: %@",testUser.username);

    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

应该是:

PFQuery *query = [PFQuery queryWithClassName:@"New"];

// BEFORE WE QUERY
[query includeKey:@"author"];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {

        // Get your objects here

    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

而且:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@",[object objectForKey:@"author"],[dateFormat stringFromDate:updated]];

应该是:

PFUser *userToDisplay = object[@"author"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@: %@",userToDisplay.username,[dateFormat stringFromDate:updated]];

因为,就目前情况而言,问题1是你说要在你已经查询解析之后包括作者。问题2是您尝试显示整个用户,需要获取用户,然后打印用户名值。