0

When I run the following code, nothing appears on my UITableView. I created a global NSMutableArray for storing the results of a query on Parse, but I can't manage to use that array to load the cells on the UITableView.

Thanks!

#import "ViewController.h"
#import "MenuViewController.h"
#import "Parse/Parse.h"

@interface ViewController ()

@end

@implementation ViewController {
    CLLocationManager *locationManager;
}

@synthesize eventTableView;
@synthesize eventTableViewCell;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadEvents];
}

- (void) loadEvents
{
    eventNames = [[NSMutableArray alloc] init];

    PFQuery *event_query = [PFQuery queryWithClassName:@"Event"];
    [event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"Successfully retrieved %lu scores.", (unsigned long)objects.count);
        for (PFObject *object in objects) {
            [eventNames addObject:[object objectForKey:@"event_name"]];
            NSLog(@"%@", [object objectForKey:@"event_name"]);
            NSLog(@"%lu", (unsigned long)eventNames.count);
        }
    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [eventNames count];
}
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    NSString *cellText = [NSString stringWithFormat:@"%@",eventNames[indexPath.row]];
    NSLog(@"%@", eventNames[indexPath.row]);

    cell.textLabel.text = cellText;

    return cell;
}

@end

2 Answers 2

1

Copy and paste the following loadEvents method

- (void) loadEvents
{
eventNames = [[NSMutableArray alloc] init];

PFQuery *event_query = [PFQuery queryWithClassName:@"Event"];
[event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
    NSLog(@"Successfully retrieved %lu scores.", (unsigned long)objects.count);
    for (PFObject *object in objects) {
        [eventNames addObject:[object objectForKey:@"event_name"]];
        NSLog(@"%@", [object objectForKey:@"event_name"]);
        NSLog(@"%lu", (unsigned long)eventNames.count);
    }
    [eventTableView reloadData];
} else {
    NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];

}

Every time you asynchronously fetch data for table view, you have to reload the whole table or the changed sections.

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

Comments

0

I'm guessing that the result from findObjectsInBackground returned after the tableview has already displayed. Did the NSLog print out the results that you expected?

One thing to try is to add a

[eventTableView reloadData];

after the for loop , which will tell the tableview to reload the data again.

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.