带有解析查询的 UITableView 未加载
UITableView With Parse Query Not Loading
所以我有一个 UITableView 和一个 Parse 查询,并且该查询能够从 parse 中检索对象。但是 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;
}
我发现可能导致问题的两件事是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
在查询之前被调用,这对我来说没有意义。
并且因为在查询之前被调用,所以 array.count 是 0;
所以我不明白为什么在查询之前会调用该行。如果您有任何建议,请告诉我!
Update这个被调用了三次,第二次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;
尝试在 viewDidLoad 中显式调用它。提供 self.myArray = objects 不 return 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);
// Configure the cell
cell.textLabel.text = [object objectForKey:@"text"];
这是什么?如果你想让它成为你查询的数组,你必须这样做:
cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.myArray objectAtIndex:indexPath.row]];
试试这个:
[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]);
}
}];
此方法:
- (PFQuery *)queryForTable
returns 一个自动填充 PFObject in cellForRowAtIndexPath:object:
in
的查询
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
但是,您所做的是在 queryForTable
方法中执行 query
。 (1) 您不需要执行查询,您只需要 return 它,但是 (2) 似乎您正在严格执行该查询以填充 self.myArray
然后将其用作 numberOfRowsInSection:
中的 return 值。 #2 的问题是您在 queryForTable
中执行的查询是异步执行的,因此在调用 numberOfRowsInSection:
时 self.myArray
可能仍为空。这就是正在发生的事情 - 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;
}
所以我有一个 UITableView 和一个 Parse 查询,并且该查询能够从 parse 中检索对象。但是 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;
}
我发现可能导致问题的两件事是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
在查询之前被调用,这对我来说没有意义。并且因为在查询之前被调用,所以 array.count 是 0;
所以我不明白为什么在查询之前会调用该行。如果您有任何建议,请告诉我!
Update这个被调用了三次,第二次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;
尝试在 viewDidLoad 中显式调用它。提供 self.myArray = objects 不 return 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); // Configure the cell cell.textLabel.text = [object objectForKey:@"text"];
这是什么?如果你想让它成为你查询的数组,你必须这样做:
cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.myArray objectAtIndex:indexPath.row]];
试试这个:
[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]);
}
}];
此方法:
- (PFQuery *)queryForTable
returns 一个自动填充 PFObject in cellForRowAtIndexPath:object:
in
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
但是,您所做的是在 queryForTable
方法中执行 query
。 (1) 您不需要执行查询,您只需要 return 它,但是 (2) 似乎您正在严格执行该查询以填充 self.myArray
然后将其用作 numberOfRowsInSection:
中的 return 值。 #2 的问题是您在 queryForTable
中执行的查询是异步执行的,因此在调用 numberOfRowsInSection:
时 self.myArray
可能仍为空。这就是正在发生的事情 - 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;
}