获取JSON数据并在iOS中填充TableView
晚上好 我需要一些帮助。我从GooglePlacesAPI获取了一些JSON格式的数据,但我没有在iPad(基于SplitView的应用程序)中填充TableView。我从iOS开发开始,所以可能会有很多错误!我使用了一个项目示例,它使用twitterapi检索帖子,并重命名了JSON数据名 我在项目中使用了四个文件并实现了该功能:获取JSON数据并在iOS中填充TableView,ios,json,tableview,json-framework,Ios,Json,Tableview,Json Framework,晚上好 我需要一些帮助。我从GooglePlacesAPI获取了一些JSON格式的数据,但我没有在iPad(基于SplitView的应用程序)中填充TableView。我从iOS开发开始,所以可能会有很多错误!我使用了一个项目示例,它使用twitterapi检索帖子,并重命名了JSON数据名 我在项目中使用了四个文件并实现了该功能: SimpleSplitController.h SimpleSplitController.m SplitSampleAppDelegate.h SplitSamp
SimpleSplitController.h
SimpleSplitController.m
SplitSampleAppDelegate.h
SplitSampleAppDelegate.m
我在SplitSampleDelegate.m文件中得到一个错误,因为它在那里被检查
如果有人能帮助我,我将非常感激
以下是实现以下功能的代码:
SplitSampleAppDelegate.h
SimpleSplitController.h
#导入
#导入“APSplitViewController.h”
#进口
@接口SimpleSplitController:APSplitViewController{
NSMutableData*响应数据;
NSArray*推特;
}
@属性(非原子,保留)UIViewController*左;
@属性(非原子,保留)UIViewController*权限;
@属性(非原子,保留)NSArray*推文;
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文;
@结束
SimpleSplitController.m
#导入“SimpleSplitController.h”
#进口
#导入“JSON/JSON.h”
#导入“Tweet.h”
@接口SimpleSplitController()
-(UIColor*)随机颜色;
-(UIViewController*)随机视图控制器1;
-(UIViewController*)随机视图控制器2;
-(UIViewController*)随机视图控制器3;
-(无效)按钮PushRandomViewController1;
-(无效)按钮PushViewController2;
@结束
@SimpleSplitController的实现
@合成左、右;
@合成推文;
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier]自动释放];
}
//配置单元格。。。
NSDictionary*aTweet=[tweets objectAtIndex:[indexPath行]];
cell.textlab.text=[aTweet objectForKey:@“name”];
cell.textlab.adjustsFontSizeToFitWidth=是;
cell.textLabel.font=[UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize=10;
cell.textlab.numberOfLines=4;
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
cell.detailTextLabel.text=[aTweet objectForKey:@“邻近区”];
NSURL*url=[NSURL URLWithString:[aTweet objectForKey:@“icon”];
NSData*data=[NSData dataWithContentsOfURL:url];
cell.imageView.image=[UIImage-imageWithData:data];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
返回单元;
}
很可能你在这里错了。您正在检查对象,而不是值
NSMutableArray *allTweets = [results valueForKey:@"results"];
乔,你的问题解决了
#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];
}
#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
#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;
}
NSMutableArray *allTweets = [results valueForKey:@"results"];