【问题标题】:Sending data from Google Places API to tableview将数据从 Google Places API 发送到 tableview
【发布时间】:2015-10-07 13:04:25
【问题描述】:

如何使用 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

【问题讨论】:

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


    【解决方案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:断点 3.1”在线通信以返回 tableview 部分中的行数:return [self.places count];。你知道为什么吗?
    • 表示[json objectForKey:@"results"]的结果不是NSArray,请NSLog查看请求结果
    • 我只改变了 NSLog(@"Google Data: %@", json);到 NSLog(@"Google 数据:%@", 地点);并且对“线程”感到失望。但是我的 tableview 还是空的...
    • [self.tableView reloadData]之前尝试记录
    • “尝试记录”是什么意思?对不起,我是初学者,我不知道在这种情况下我必须在哪里登录......
    【解决方案2】:

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

    self.places = [json objectForKey:@"results"];
    [self.tableView reloadData];
    

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 2023-01-16
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多