2012-07-13 103 views
0

我必须在具有表格视图的iPhone应用程序的Tabbar控制器中添加一个选项卡什么是显示JSON数据。表格视图中的JSON数据iOS

我的问题是现在,我看到表视图,但没有数据。这里是我的代码:

.h文件中

#import <UIKit/UIKit.h> 

@interface FourthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

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

@end 

.m文件

#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 

我已经使用这个啧啧这里:http://jeffmenter.wordpress.com/2012/05/30/json-ios-5-go/

+0

顺便说一句,当你NSLog(@“twitter:%@”,twitter)时输出是什么? – 2012-07-13 08:17:55

回答

0

因为它是当前定义的鸣叫是一种NSArray的。您不应使用objectForKey访问其成员,而应使用objectAtIndex:indexPath.row。 (在cellForRowAtIndexPath

根据您收到的数据,您可能会选择使用NSDictionara进行推文。

0

检查tweet中的objectforkey“text”是否为字符串,如果不是使用[NSString Stringwithformat:@“%@”,[tweet objectForKey:@“text”]]将其转换为字符串;

0

您应该将该JSON数据放在表视图之外的对象中,然后将其提取到表视图cellForRowAtIndexPath方法中,而不是直接获取它。 由于下载数据需要一些时间,连接不良也是一个问题。所以如果数据没有下载,表格视图将不显示任何内容。因此,最好下载它并检查它是否通过添加断点来存在,而不是尝试获取它。