简体   繁体   English

带有解析查询的UITableView未加载

[英]UITableView With Parse Query Not Loading

So I have a UITableView and a Parse query, and the query is able to retrieve the objects from parse. 所以我有一个UITableView和一个解析查询,该查询能够从解析中检索对象。 But the TableView is no showing them. 但是TableView没有显示它们。

Here is my code I'll explain more below: 这是我的代码,下面将详细说明:

- (PFQuery *)query {
    NSLog(@"hello");
    PFQuery *query = [PFQuery queryWithClassName:@"Posts"];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.


    // Query for posts near our current location.

    // Get our current location:
    //CLLocation *currentLocation = [self.dataSource currentLocationForWallPostsTableViewController:self];
    CLLocationAccuracy filterDistance = [[NSUserDefaults standardUserDefaults] doubleForKey:PAWUserDefaultsFilterDistanceKey];

    // And set the query to look by location
    PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:40.941984
                                               longitude:-72.88712399999997];
    [query whereKey:PAWParsePostLocationKey nearGeoPoint:point withinKilometers:PAWMetersToKilometers(filterDistance)];
    [query includeKey:PAWParsePostUserKey];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count);
            self.myArray = objects;
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];


    NSLog(@"work");

    return query;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

    return self.myArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSLog(@"yy");
    NSString *kk= [object objectForKey:@"text"];
    NSLog(@"%@",kk);
    // Configure the cell
    cell.textLabel.text = [object objectForKey:@"text"];

    return cell;
}

Two things that I have found out that may be causing the issue is: 我发现可能导致此问题的两件事是:

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is being called before the query, which doesn't make sense to me. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section在查询之前调用了- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ,这对我来说没有意义。

  2. And because that is being called before the query the array.count is 0; 并且因为在查询之前调用了该方法,所以array.count为0;

So I don't understand why that line would be called before the the query. 所以我不明白为什么在查询之前会调用该行。 If you have any suggestions please let me know! 如果您有任何建议,请让我知道!

Update This is being called three times, and the second nslog is not being called. 更新这被调用了三次,第二个nslog没有被调用。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        NSLog(@"Fsa");
        return self.myArray.count;
        NSLog(@"Successfully retrieved %lu .", (unsigned long)self.myArray.count);

    }

In my .h 在我的.h中

UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

在此处输入图片说明

This method: 该方法:

- (PFQuery *)queryForTable

returns a query which automatically populates the PFObject in cellForRowAtIndexPath:object: in 返回一个查询,该查询自动在cellForRowAtIndexPath:object:中填充PFObject

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

What you've done, however, is perform the query within your queryForTable method. 但是,您要做的是在queryForTable方法中执行query (1) You don't need to perform the query, you simply need to return it, but (2) it seems as if you're strictly performing that query in order to populate self.myArray which you then which to use as a return value in numberOfRowsInSection: . (1)您不需要执行查询,只需要返回它,但是(2)似乎严格执行该查询是为了填充self.myArray ,然后将其用作以numberOfRowsInSection:返回的值。 The problem with #2 is that the query you're performing in queryForTable is performed asynchronously, so self.myArray may still be empty by the time numberOfRowsInSection: is called. #2的问题在于,您在queryForTable中执行的查询是异步执行的,因此self.myArraynumberOfRowsInSection:调用时可能仍然为空。 So that's what's happening -- self.myArray.count = 0 and therefore cellForRowAtIndexPath: wouldn't be called being called. 这就是发生的事情self.myArray.count = 0,因此cellForRowAtIndexPath:不会被称为被调用。

But the biggest problem of all, #3, is that - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object can only be used in a PFQueryTableViewController so you'll have to use a query and the standard UITableView delegate methods instead. 但所有问题中的最大#3是- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object只能在PFQueryTableViewController使用,因此您将拥有而是使用查询和标准的UITableView委托方法。

Try this instead: 尝试以下方法:

- (void)viewDidLoad {

    NSLog(@"hello");
    PFQuery *query = [PFQuery queryWithClassName:@"Posts"];

    // Query for posts near our current location.

    // Get our current location:
    //CLLocation *currentLocation = [self.dataSource currentLocationForWallPostsTableViewController:self];
    CLLocationAccuracy filterDistance = [[NSUserDefaults standardUserDefaults] doubleForKey:PAWUserDefaultsFilterDistanceKey];

    // And set the query to look by location
    PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:40.941984
                                               longitude:-72.88712399999997];
    [query whereKey:PAWParsePostLocationKey nearGeoPoint:point withinKilometers:PAWMetersToKilometers(filterDistance)];
    [query includeKey:PAWParsePostUserKey];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count);
            self.myArray = objects;

            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSLog(@"yy");
    NSString *kk= [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"text"];
    NSLog(@"%@",kk);
    // Configure the cell
    cell.textLabel.text = [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"text"];

    return cell;
}

Try this: 尝试这个:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count);
        self.myArray = objects;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

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

Try explicitly calling it in viewDidLoad. 尝试在viewDidLoad中显式调用它。 Provided self.myArray = objects doesn't return nil this should suffice. 提供的self.myArray =对象不返回nil,这就足够了。 It will force it to load circumventing other methods loading first: 它将迫使它避免其他首先加载的方法加载:

-(void)viewDidLoad {
    ...
    [self locationQuery];
}

-(PFQuery *)locationQuery {

    CLLocationAccuracy filterDistance = [[NSUserDefaults standardUserDefaults] doubleForKey:PAWUserDefaultsFilterDistanceKey];
    PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:40.941984
                                           longitude:-72.88712399999997];

    PFQuery *query = [PFQuery queryWithClassName:@"Posts"];
    [query whereKey:PAWParsePostLocationKey nearGeoPoint:point withinKilometers:PAWMetersToKilometers(filterDistance)];
    [query includeKey:PAWParsePostUserKey];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                // The find succeeded.
                NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count); 
                self.myArray = objects;
                //OR : 
                self.myArray = [objects valueForKey:@"NameOfParse.comClassColumnHere"];
                [self.tableView reloadData];
            } else {
                // Log details of the failure
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
     }];
        return query;
}

Additionally, your cell.textLabel.text is referencing something that doesn't exist.. 此外,您的cell.textLabel.text引用的内容不存在。

NSString *kk= [object objectForKey:@"text"]; NSString * kk = [object objectForKey:@“ text”]; NSLog(@"%@",kk); 的NSLog(@ “%@”,KK); // Configure the cell cell.textLabel.text = [object objectForKey:@"text"]; //配置单元格cell.textLabel.text = [object objectForKey:@“ text”];

what is this? 这是什么? if you want it to be the array you queried you have to do : 如果要使其成为您查询的数组,则必须执行以下操作:

cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.myArray objectAtIndex:indexPath.row]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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