将Google Places API中的数据发送到tableview

时间:2015-10-07 13:04:25

标签: ios objective-c google-places-api

如何使用objective-C将数据发送到iOS中的tableview?我试图解决我的问题很长时间但结果不正确。我的桌面视图仍然是空的。我做错了什么?以下是我的实施文件。

import "PlacesViewController.h"

@implementation PlacesViewController
@synthesize places;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set this view controller object as the delegate and data source for the table view
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
}

-(void)queryGooglePlaces
{
    //Build the url string to send to Google
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.503186,-0.126446&radius=5000&types=food@|restaurant|bar&keyword=vegetarian&key=myOwnKEY"];
    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];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self fetchedData:data];
        });
    });
}

-(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".
    self.places = [json objectForKey:@"results"];

    //Write out the data to the console.
    NSLog(@"Google Data: %@", json);
}

#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"];
    return cell;
}

@end

2 个答案:

答案 0 :(得分:1)

每当您更新数据源时,都需要调用[self.tableView reloadData]

所以应该是这样的

-(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".
    self.places = [json objectForKey:@"results"];

    // NOTE - ADDED RELOAD
    [self.tableView reloadData];

    //Write out the data to the console.
    NSLog(@"Google Data: %@", json);
}

有关reloadData的更多信息,请参阅the answer

答案 1 :(得分:0)

更新数据源后,应重新加载表视图。

GetViewForAnnotation