Question

I am trying to use (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object to customize the display of PFObject data in a PFQueryTableViewController. So I construct my PFQuery object in (PFQuery *)queryForTable in my PFQueryTableViewController delegate that is supposed to obtain the multiple objects in an NSArray.

I thought that parse.com is supposed to then send/call (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object once for each of the objects returned. However, I find that it keeps returning the same object multiple times.

Thus, for example, instead of 3 different objects for the 3 times (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object is called, I get the same object. But it is not a problem with the data or the query, because at the end of (PFQuery *)queryForTable , just before

return query;

I tried

[query findObjectsInBackgroundWithBlock:^(NSArray *resultsNow,NSError *error) {NSLog(@"We have the following: %@", resultsNow);}];
 sleep(5);

and this shows the 3 objects obtained correctly. How is it that the exact same query when handled by PFQueryTableViewController calling (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 3 times, instead of sending the 3 objects one by one, it sends the first one 3 times?

In response to Wain's questions, I added some answers in the comments, but also the following seems relevant:

- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath {
// overridden, since we want to implement sections
if (indexPath.section < self.objects.count) {
    return [self.objects objectAtIndex:indexPath.section];
}

return nil;
}
Was it helpful?

Solution 2

Thanks to Wain's question, I logged objectsDidLoad: and found that the correct 3 objects were loading, so it had to be a matter of the PFQueryTableViewController just not loading them in the right sequence in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object.

In particular, the mapping was supposed to be in -(PFObject *)objectAtIndex:(NSIndexPath *)indexPath as indicated in my question. But upon looking at the documentation for the Nth time, I suddenly noticed that the method name should actually be -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath. No wonder it wasn't being called, and my mapping was coming into effect, and hence the wrong object was being passed to (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object. When I changed it to -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath, the problem went away!

How could I have got the wrong method name? I found that I had copied that mapping example from the anypic code, so I'm guessing that parse.com changed the method name sometime after anypic was written? Can anyone verify that? I'm using parse framework 1.2.14

OTHER TIPS

I had a similar issue and what I did was:

1 - Instead of using PFQueryTableViewController just use regular UITableViewController.

2 - Change the method queryForTable:to (void).

- (void)queryForTable {

3 - Delete PFQueryTableViewmethods and implement DataSourcemethods.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    }

Call [self queryForTable] in viewDidLoad after declaring it in .h file.

If you don't use block, you just create NSArray property and populate on queryForTable:

self.yourarray = [query findobjects];

If you use block, just make sure you populate NSArrayinside block and reload tableview.

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

    if (error) {
        // There was an error, do something with it.
    }
    else {
        self.yourArray = objects;
    }
    // Reload your tableView Data
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    }); 
}];

This is how I got it working. Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top