ios - 将数据从 Google Places API 发送到 tableview
<p><p>如何使用 Objective-C 将数据发送到 iOS 中的 tableview?我试图解决我的问题很长时间,但结果不正确。我的表格 View 仍然是空的。我做错了什么?下面是我的实现文件。</p>
<pre><code>import "PlacesViewController.h"
@implementation PlacesViewController
@synthesize places;
- (void)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 = ;
url = ;
NSLog(@"%@", url);
//Formulate the string as a URL object.
NSURL *googleRequestURL=;
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = ;
dispatch_sync(dispatch_get_main_queue(), ^{
;
});
});
}
-(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 = ;
//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 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = ;
NSDictionary *tempDictionary= ;
cell.textLabel.text = ;
return cell;
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>每当更新数据源时,都需要调用<code></code></p>
<p>所以应该是这样的</p>
<pre><code>-(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 = ;
// NOTE - ADDED RELOAD
;
//Write out the data to the console.
NSLog(@"Google Data: %@", json);
}
</code></pre>
<p>有关 <code>reloadData</code> 的更多信息,请参阅 <a href="https://stackoverflow.com/a/10823449/2956647" rel="noreferrer noopener nofollow">the answer</a> </p></p>
<p style="font-size: 20px;">关于ios - 将数据从 Google Places API 发送到 tableview,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32992997/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32992997/
</a>
</p>
页:
[1]