因此,我有一个称为“queryParseForRelevantData”的函数。此方法查询解析并获取PF对象的数据并将数据存储在NSMutable数组中。我希望这些可变数组中的一个然后显示在表中。如何在生成表之前执行查询?
以下是我正在使用的代码
#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;
}
答案 0 :(得分:1)
正如DrGodCarl所说,看起来你想在信息加载后刷新表格。通常,这可以通过使用块/完成处理程序以多种方式完成。
以下是使用完成处理程序的示例:
- (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检索数据后才能加载表。