• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将数据从 Google Places API 发送到 tableview

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:12:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap