在 iOS 中获取 JSON 数据并填充 TableView
Posted
技术标签:
【中文标题】在 iOS 中获取 JSON 数据并填充 TableView【英文标题】:Getting JSON data and populate TableView in iOS 【发布时间】:2011-10-08 03:58:56 【问题描述】:晚上好!
我需要一些帮助。我从 Google Places API 获取一些 JSON 格式的数据,但我没有在 iPad 中填充 TableView(基于拆分视图的应用程序)。我从 ios 开发开始,所以可能有很多错误!我使用了一个项目示例,该示例使用 Twitter API 来检索帖子并只是重命名了 JSON 数据名称。
我有四个文件在项目中使用并实现功能:
SimpleSplitController.h
SimpleSplitController.m
SplitSampleAppDelegate.h
SplitSampleAppDelegate.m
我在 SplitSampleDelegate.m 文件中收到一个错误,因为它在那里进行了检查...
如果有人可以帮助我,我将非常感激!
以下是实现的代码:
SplitSampleAppDelegate.h
#import <UIKit/UIKit.h>
@class APTabBarControllerForSplitController;
@interface SplitSampleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
NSMutableData *responseData;
NSMutableArray *tweets;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet APTabBarControllerForSplitController *tabBarController;
@property (nonatomic, retain) NSMutableArray *tweets;
@end
SplitSampleAppDelegate.m
#import "SplitSampleAppDelegate.h"
#import "APTabBarControllerForSplitController.h"
@implementation SplitSampleAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize tweets;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.tabBarController.delegate = self;
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
// Add the view controller's view to the window and display.
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=restaurant&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[responseData setLength:0];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[responseData appendData:data];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *results = [responseString JSONValue];
NSMutableArray *allTweets = [results objectForKey:@"results"];
//This is the part that I get an ERROR
[viewController setTweets:allTweets];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
SimpleSplitController.h
#import <UIKit/UIKit.h>
#import "APSplitViewController.h"
#import <MapKit/MapKit.h>
@interface SimpleSplitController : APSplitViewController
NSMutableData *responseData;
NSArray *tweets;
@property (nonatomic, retain) UIViewController *left;
@property (nonatomic, retain) UIViewController *right;
@property (nonatomic, retain) NSArray *tweets;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
@end
SimpleSplitController.m
#import "SimpleSplitController.h"
#import <QuartzCore/QuartzCore.h>
#import "JSON/JSON.h"
#import "Tweet.h"
@interface SimpleSplitController()
- (UIColor *) randomColor;
- (UIViewController*) randomViewController1;
- (UIViewController*) randomViewController2;
- (UIViewController*) randomViewController3;
- (void) buttonPushRandomViewController1;
- (void) buttonPushRandomViewController2;
@end
@implementation SimpleSplitController
@synthesize left, right;
@synthesize tweets;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
NSDictionary *aTweet = [tweets objectAtIndex:[indexPath row]];
cell.textLabel.text = [aTweet objectForKey:@"name"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [aTweet objectForKey:@"vicinity"];
NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"icon"]];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
【问题讨论】:
【参考方案1】:你可能在这里错了。您正在检查对象,而不是值。
NSMutableArray *allTweets = [results valueForKey:@"results"];
你的问题已经解决了。
【讨论】:
以上是关于在 iOS 中获取 JSON 数据并填充 TableView的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 使用 Swift 在每个表格视图单元格中获取 JSON 提要
Phonegap iOS 应用程序 - 使用 csv 或 JSON 创建和填充本地数据库?
如何在异步获取 JSON 数据以填充到 UITableView 中时显示 UIActivityIndicatorView?