OStack程序员社区-中国程序员成长平台

标题: ios - 将数据从 Google Places API 发送到 tableview [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:12
标题: ios - 将数据从 Google Places API 发送到 tableview

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

import "lacesViewController.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)fetchedDataNSData *)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)numberOfSectionsInTableViewUITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    // Return the number of rows in the section.
    return [self.places count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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



Best Answer-推荐答案


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

所以应该是这样的

-(void)fetchedDataNSData *)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

关于ios - 将数据从 Google Places API 发送到 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32992997/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4