1

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;
}
7
  • 1
    What index paths are sent? What is provided to objectsDidLoad: / available in objects? Commented Jan 8, 2014 at 16:11
  • Thanks, Wain, for the good question. At first glance, it appears that objectsDidLoad: is just calling super and stuff like self.tableView.tableHeaderView = nil; and self.tableView.scrollEnabled = YES; that shouldn't make a difference, but I'll explore that more. Commented Jan 8, 2014 at 16:34
  • As for the index paths, they are (in order, 1st, 2nd and 3rd time), <NSIndexPath 0x1e86b5e0> 2 indexes [0, 0], <NSIndexPath 0x1e84db30> 2 indexes [1, 0] and <NSIndexPath 0x1d5ce8f0> 2 indexes [0, 0] Commented Jan 8, 2014 at 16:35
  • 1
    I mean you can log what gets received in objectsDidLoad: before the cells are updated. Commented Jan 8, 2014 at 16:43
  • Thanks Wain, really appreciate it! Sorry I have to go now, will take a look and update this another time with your suggestion. thanks again Commented Jan 8, 2014 at 16:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jorge, but I would prefer to be able to get PFQueryTableViewController to work if possible, since it comes with some other conveniences.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.