无法使用Google Places API的数据填充ViewController(而非TableViewController)中的TableView。
我已经成功地提取了数据并将JSON解析为带有批注的MKMapView,所以我知道该部分正在工作,但是我希望数据也能流到它下面的表中(见照片)。
我到处都在寻找有关如何执行此操作的简单说明。甚至遵循了一个教程(但这是为TableViewController设计的,所以我希望我的问题就是我将其链接的方式)。另一种可能性是放置我的[self.tableView reloadData];
调用。这对我来说是合乎逻辑的,但很可能在错误的地方。显然,如果不在正确的位置,TableView将保持空白,但是我已经在几个地方尝试过了。
注意:我只包含了相关代码,希望可以使你们更容易发现。 TableView对象作为其数据源和委托连接到ViewController,在#pragma代码中,在属性编辑器中将原型单元称为“Cell”,并将JSON数据存储在称为“places”的NSArray中。如果您发现我错过了一些愚蠢的东西而导致它无法正常工作,请提前告诉我。
ViewController.h
@interface RendezvousViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSArray *places;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
ViewController.m
-(void)viewDidLoad {
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
-(void) queryGooglePlaces: (NSString *) googleType {
// Build the url string to send to Google.
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=%@&types=food|restaurant|bar&sensor=true&keyword=%@&key=%@",currentCentre.latitude,currentC
entre.longitude,[NSString stringWithFormat:@"%i",currenDist],selection, kGOOGLE_API_KEY];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", url);
//Formulate the string as a URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
//The results from Google will be an array obtained from the NSDictionary object with the key "results".
NSArray* places = [json objectForKey:@"results"];
//Write out the data to the console.
NSLog(@"Google Data: %@", places);
[self plotPositions:places];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.places count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tempDictionary= [self.places objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:@"name"];
if([tempDictionary objectForKey:@"rating"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"Rating: %@ of 5",[tempDictionary objectForKey:@"rating"]];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"Not Rated"];
}
return cell;
}
最佳答案
看来您的dispatch_async
调用出现了问题。
现在,一旦获得数据,就应该在mainThread
中填充它。
您必须将 call 修改为:
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
dispatch_sync(dispatch_get_main_queue(), ^{
[self fetchedData:data];
});
});
这将确保仅当您在后台线程中完成了从Google加载数据后,才调用
[self fetchedData:data];
。这是我的live示例的类似结果。
代码中也有一个错误:将
NSArray* places = [json objectForKey:@"results"];
更改为self.places = [json objectForKey:@"results"];
。希望能有所帮助。
关于ios - 将API数据发送到ViewController中的UITableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21143428/