2012-07-13 6 views
0

JSON 데이터를 보여주는 테이블 뷰가있는 아이폰 앱의 Tabbar 컨트롤러에 탭을 추가해야합니다.JSON 데이터 (테이블 뷰 포함) iOS

내 문제는 이제 테이블보기가 표시되지만 데이터가 표시되지 않습니다.

.H 파일

#import <UIKit/UIKit.h> 

@interface FourthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

    @property (strong, nonatomic) IBOutlet UITableView *myTableView; 
    @property (nonatomic, strong) NSArray *tweets; 

@end 

내가 여기이 혀를 사용했습니다

#import "FourthViewController.h" 

@interface FourthViewController() 

@end 

@implementation FourthViewController 

@synthesize myTableView; 
@synthesize tweets; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Issue load request after the view has been loaded. 
    [self issueLoadRequest]; 
} 

- (void)viewDidUnload 
{ 
    [self setMyTableView:nil]; 
    [super viewDidUnload]; 
} 

- (void)issueLoadRequest 
{ 
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=jmenter&count=10"]]; 
     [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES]; 
    }); 
} 

- (void)receiveData:(NSData *)data { 
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is 
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data. 
    self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    [self.myTableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.tweets.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc] init]; 
    // The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text". 
    NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [tweet objectForKey:@"text"]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:11]; 
    cell.textLabel.numberOfLines = 3; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Spit out some pretty JSON for the tweet that was tapped. Neato. 
    NSString *formattedJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[self.tweets objectAtIndex:indexPath.row] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]; 
    NSLog(@"tweet:\n%@", formattedJSON); 
} 


@end 

하는 .m 파일이 : http://jeffmenter.wordpress.com/2012/05/30/json-ios-5-go/이 현재 정의 된 바와 같이

+0

쓸데 없는데, NSLog (@ "twitter : % @", twitter) 때 출력은 무엇입니까? –

답변

0

이 트윗은 여기 내 코드입니다 NSArray. objectForKey을 사용하고 objectAtIndex:indexPath.row을 사용하여 회원에 액세스하면 안됩니다. (cellForRowAtIndexPath)

받는 데이터에 따라 짹짹을 위해 NSDictionara를 선택할 수 있습니다.

0

트윗의 objectforkey "text"가 [NSString Stringwithformat : @ "% @", [tweet objectForKey : @ "text"]]를 사용하여 문자열로 변환하지 않는지 확인하십시오.

0

테이블 뷰 외부의 개체에서 JSON 데이터를 가져 와서 테이블 뷰 cellForRowAtIndexPath 메서드로 가져오고 직접 가져 오지 마세요. 데이터 다운로드에는 시간이 오래 걸리고 연결 불량도 문제가되기 때문에. 따라서 데이터가 다운로드되지 않으면 테이블보기에 아무 것도 표시되지 않습니다. 따라서 데이터를 가져 오려고 시도하는 것보다 중단 점을 추가하여 데이터가 있으면이를 다운로드하고 확인하는 것이 좋습니다.

관련 문제