Search code examples
iosobjective-cuitableviewparse-platformpfquery

How to retrieve just one column from Parse PFQuery. " selectKeys" not working


I have a list of TV programs in a Parse class called "Programs". Each program has the fields:

-programTitle

-programDescription

-airsOn (this is the network it airs on)

I'm trying to display a list of all the "programTitle"s in a tableview so that the user can select the program they want to see detail on. However, when I run the code below to select only the "programTitle" column, I'm getting 3 columns of data back. The "programTitle" as well as a "localId" and "objectId".

How can I download only the list of "programTitle"s from the Parse class and display them in a Tableview?

My code and output are below. You'll see I have a property called "programList" in the header file. I put the query result from my viewDidLoad into the property "programList" and then print it using NSLog. As you can see from the output, fail. Parse documentation has confused me more. Please help.

Header File:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface ProgramsTableViewController : UITableViewController
@property(nonatomic,strong) NSArray *programList;
@property(nonatomic) NSString *selectedProgram;

@end

viewDidLoad in implementation file:

- (void)viewDidLoad {
    [super viewDidLoad];

    PFQuery *query = [PFQuery queryWithClassName:@"Programs"];
    [query selectKeys:@[@"programTitle"]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.programList = objects;
            NSLog(@"%@",self.programList);
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

Output:

2015-05-03 13:59:35.233 Oingo[27838:1802357] (
    "<Programs: 0x7f94a3c608a0, objectId: ukcFRmDsb1, localId: (null)> {\n    programTitle = \"Silicon Valley\";\n}",
    "<Programs: 0x7f94a3c609b0, objectId: JvW9oAYlo8, localId: (null)> {\n    programTitle = \"Broad City\";\n}",
    "<Programs: 0x7f94a3c80920, objectId: n1LJE3YWoP, localId: (null)> {\n    programTitle = \"Mindy Project\";\n}"

tableView methods that don't work:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return [self.programList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = [self.programList objectAtIndex:indexPath.row];
    return cell;
}

Solution

  • You could filter the list you receive to pull out the titles:

    self.programList = [objects valueForKey:@"programTitle"]
    

    or you can keep the objects you have in the array and take out the title for display:

    cell.textLabel.text = [[self.programList objectAtIndex:indexPath.row] objectForKey:@"programTitle"];
    

    or you could write a cloud code function to get the list and then return just the list of titles.