我正在尝试将Google Places中的名称详细信息放到表格视图中,但我在UIViewController中的tableview仍然是空的。我获取数据,解析JSON,我在控制台中看到结果,但我无法进入表视图。请告诉我我哪里错了。我觉得我非常接近解决方案。这是结果搜索:
"html_attributions" : [],"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.86879,
"lng" : 151.194217
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
"name" : "Rhythmboat Cruises",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 480,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104066891898402903288\"\u003eRhythmboat Cruises\u003c/a\u003e"
],
"photo_reference" : "CmRdAAAAWYCQgAwmmu-5Uzelh1besNbSTJyF91IQgS01fZUDgHD8fBu9Y9NGZPjcw9Dt77HVVaSjlnQxbHQhBto_RKmOXs5zb3otmO6YU9z-FdXeVXlRTZGoaC999YS6_5YLBXskEhCCIO6boLQBNEzlGuCKSsemGhTmKEG6Hkgb3YtOGo1HZK6V3sK4ZQ",
"width" : 640
}
],
"place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
"reference" : "CnRmAAAAwQ30xJ1IlDWUexRFJl3deTHQo20g1e5mv9aFjQW-UNak4Xi2_f-rirQrTOYK8xxVMpQdf8lI9FMCWLFMKOW3dmXBlIo-RyprHgHcv1GlyC-WwUMlJdu9rwPm4fMOsyHuF3Ftu2-VzJfFJdlSi698EhIQgQhM7DyZnZdttt6nWcyysRoU5TYhn0mo6C0JQsrb2AOOuZB0uUE",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Pyrmont Bay Wharf (Near Australia Maritime Museum), Pyrmont, NSW 2009"
},
这是我的ViewController.h
#import <UIKit/UIKit.h>
//#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
@interface PlacesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableData *connectionData;
@property (nonatomic, strong) NSArray *messageArray;
@property (strong, nonatomic) IBOutlet UITableView *messageTable;
@property (strong, nonatomic) IBOutlet UIView *activityView;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
和我在ViewController.m中的实现
#import "PlacesViewController.h"
//#import <CoreLocation/CoreLocation.h>
#import "Places.h"
#define kMessageBoardURLString @"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=myAPI_KEY"
@interface PlacesViewController ()
@end
@implementation PlacesViewController
@synthesize connectionData;
@synthesize messageArray;
@synthesize messageTable;
@synthesize activityView;
@synthesize activityIndicator;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.connectionData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.connectionData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
connection = nil;
connectionData = nil;
NSLog(@"Connect error. Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *retString =
[NSString stringWithUTF8String:[connectionData bytes]];
NSLog(@"Return json: %@", retString);
NSError *parseError = nil;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:connectionData
options:0
error:&parseError];
if (!parseError) {
self.messageArray = [json objectForKey:@"results"];
NSLog(@"Table of json to: %@", json);
[messageTable reloadData];
} else {
NSString *err = [parseError localizedDescription];
NSLog(@"parse error: %@", err);
}
connection = nil;
connectionData = nil;
[self.activityView setHidden:YES];
[self.activityIndicator stopAnimating];
}
#pragma mark - Table data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
NSDictionary *message = [self.messageArray objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"name"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self messageArray] count];
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setMessageTable:nil];
[self setActivityView:nil];
[self setActivityIndicator:nil];
[super viewDidUnload];
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"viewWillAppear, refreshing messages");
//start activity indicator
[self.activityView setHidden:NO];
[self.activityIndicator startAnimating];
NSURL *msgURL = [NSURL URLWithString:kMessageBoardURLString];
NSURLRequest *msgRequest =
[NSURLRequest requestWithURL:msgURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection =
[[NSURLConnection alloc] initWithRequest:msgRequest
delegate:self];
if (theConnection) {
NSMutableData *connData = [[NSMutableData alloc] init];
[self setConnectionData:connData];
} else {
NSLog(@"Cannot connect...");
[self.activityView setHidden:YES];
[self.activityIndicator stopAnimating];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
connectionData = nil;
}
@end