繁体   English   中英

带有解析查询的UITableView未加载

[英]UITableView With Parse Query Not Loading

所以我有一个UITableView和一个解析查询,该查询能够从解析中检索对象。 但是TableView没有显示它们。

这是我的代码,下面将详细说明:

- (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;
}

我发现可能导致此问题的两件事是:

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section在查询之前调用了- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ,这对我来说没有意义。

  2. 并且因为在查询之前调用了该方法,所以array.count为0;

所以我不明白为什么在查询之前会调用该行。 如果您有任何建议,请让我知道!

更新这被调用了三次,第二个nslog没有被调用。

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

    }

在我的.h中

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

在此处输入图片说明

该方法:

- (PFQuery *)queryForTable

返回一个查询,该查询自动在cellForRowAtIndexPath:object:中填充PFObject

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

但是,您要做的是在queryForTable方法中执行query (1)您不需要执行查询,只需要返回它,但是(2)似乎严格执行该查询是为了填充self.myArray ,然后将其用作以numberOfRowsInSection:返回的值。 #2的问题在于,您在queryForTable中执行的查询是异步执行的,因此self.myArraynumberOfRowsInSection:调用时可能仍然为空。 这就是发生的事情self.myArray.count = 0,因此cellForRowAtIndexPath:不会被称为被调用。

但所有问题中的最大#3是- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object只能在PFQueryTableViewController使用,因此您将拥有而是使用查询和标准的UITableView委托方法。

尝试以下方法:

- (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;
}

尝试这个:

[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]);
    }
}];

尝试在viewDidLoad中显式调用它。 提供的self.myArray =对象不返回nil,这就足够了。 它将迫使它避免其他首先加载的方法加载:

-(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;
}

此外,您的cell.textLabel.text引用的内容不存在。

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

这是什么? 如果要使其成为您查询的数组,则必须执行以下操作:

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

暂无
暂无

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

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