[英]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;
}
我發現可能導致此問題的兩件事是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
在查詢之前調用了- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
,這對我來說沒有意義。
並且因為在查詢之前調用了該方法,所以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.myArray
在numberOfRowsInSection:
調用時可能仍然為空。 這就是發生的事情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.