简体   繁体   中英

Parse SDK with iOS Device: Can't Load Data from Query onto UITableView

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM