2011-10-08 2 views
0

안녕하세요!JSON 데이터 가져 오기 및 iOS에서 TableView 채우기

도움이 필요합니다. Google Places API의 일부 데이터가 JSON 형식으로 표시되지만 iPad에서 TableView를 채울 수 없습니다 (SplitView 기반 애플리케이션). iOS 개발로 시작해서 아마 많은 실수가있을 것입니다! Twitter API를 사용하여 게시물을 검색하고 JSON 데이터 이름의 이름을 바꾼 프로젝트 예제를 사용했습니다.

나는이 프로젝트에 사용하고 기능을 구현하는 네 개의 파일이 있습니다 거기 확인할 것 같이 내가 SplitSampleDelegate.m 파일에 오류가

SimpleSplitController.h 
SimpleSplitController.m 
SplitSampleAppDelegate.h 
SplitSampleAppDelegate.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"]; 

문제가 해결되었습니다.

관련 문제