在视图加载到 UItableView 之前获取要执行的 Parse 查询方法

Getting a Parse query method to execute before view did load in a for UItableView

所以,我有一个称为 "queryParseForRelevantData" 的函数,此方法查询解析并获取 PF 对象的数据,并将数据存储在 NSMutable 数组中。我希望这些 mutable 数组之一随后显示在 table 中。如何在生成 table 之前执行查询?

这是我使用的代码

#import "ResultsTableViewController.h"
#import "Parse/Parse.h"

@interface ResultsTableViewController ()

@end

@implementation ResultsTableViewController
{




}




- (void)viewDidLoad {
[self queryParseForRelevantData];
[super viewDidLoad];
_tableData = _flushTypeArray;


NSLog(@"why must you hate me %@",_dataPointNumberArray);





// NSLog(@"%@", _COMBINEDNAMES);

}
-(void)queryParseForRelevantData{
_dataPointNumberArray = [[NSMutableArray alloc] init];
_flushTypeArray = [[NSMutableArray alloc] init];
_tableData = [[NSMutableArray alloc] init];
_heightDifferenceArray = [[NSMutableArray alloc] init];
_variableArray = [[NSMutableArray alloc] init];
_massOfWaterArray = [[NSMutableArray alloc] init];


PFQuery *query = [PFQuery queryWithClassName:@"DataPoint"];
//queriess the class DataPoint to find when instances of "user" equal the
//logged in user's username
[query whereKey:@"userName" equalTo:_username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.

        NSLog(@"%@", objects);

        NSLog(@"%@", objects.class);
        NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count);


        // Do something with the found objects


        if (objects.count == 0) {
            //uialert letting the user know that no user matches the query
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Prior Data"
                                                            message:@"No Prior Data"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];








        }
        //if there is only one number matching the query

        if (objects.count >=1) {




            for (PFObject *object in objects) {

                // NSLog(@"%@", objects);
                // NSLog(@"%@", object.class);

                NSDictionary *dpn = object[@"dataPointNumber"];
                NSDictionary *ft = object[@"flushType"];
                NSDictionary *hd= object [@"heightDifference"];
                NSDictionary *var = object [@"variable"];
                NSDictionary *mow = object[@"massOfWater"];






                if  (dpn != NULL){
                    NSLog(@"Yo bro here is Dpn   %@",dpn);


                    [_dataPointNumberArray addObject:dpn];
                    NSLog(@"number: %@", _dataPointNumberArray);
                }
                if (dpn == NULL) {
                    [_dataPointNumberArray addObject:@"Blank"];
                    NSLog(@"Blank space");

                }
                if (ft != NULL) {
                    [_flushTypeArray addObject:ft];
                    [_tableData addObject:ft];
                    NSLog(@"Flush Type: %@",_flushTypeArray);
                     NSLog(@"the table data is %@",_tableData);
                }
                if (ft == NULL) {
                    [_flushTypeArray addObject:@"Blank"];
                    NSLog(@"Blank space");

                }
                if (hd !=NULL){
                    [_heightDifferenceArray addObject:hd];
                    NSLog(@"height Difference: %@", _heightDifferenceArray);
                }
                if (hd ==NULL){
                    [_heightDifferenceArray addObject:@"blank"];
                    NSLog(@"Blank Space");
                }
                if (var!=NULL) {
                    [_variableArray addObject:var];
                    NSLog(@"Variable: %@", _variableArray);
                }
                if (var == NULL) {
                    [_variableArray addObject:@"blank"];
                    NSLog(@"Blank Space");

                }


                if (mow != NULL) {
                    [_massOfWaterArray addObject:mow];
                    NSLog(@"Mass of water: %@",_massOfWaterArray);
                }
                if (mow == NULL) {
                    [_massOfWaterArray addObject:@"blank"];
                    NSLog(@"Blank Space");
                }


            }


        }

        else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }}];

   }



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

return [_tableData count];

// Return the number of rows in the section.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

cell.textLabel.text = [_tableData objectAtIndex:indexPath.row];
return cell;

}

正如 DrGodCarl 所说,您似乎想在信息加载后刷新 table。通常,这可以通过使用 blocks/completion 处理程序以多种方式完成。

这是一个使用完成处理程序的示例:

- (void)queryParseForRelevantData:(void (^)(NSArray *))completionHandler
{
    // Run your PFQuery's

    PFQuery *query = [PFQuery queryWithClassName:@"DataPoint"];
    [query whereKey:@"userName" equalTo:_username];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // Query found all your objects, return the completion handler
        completionHandler(objects);
    }
}

要调用此方法:

- (void)viewDidLoad
{
    [self queryParseForRelevantData:^(NSArray *arrayOfDataPoints) {
        // Update the table with the results.

    }];

}

只有在从 Parse 中检索到数据后,您才能加载 table。