0

我有这个问题。我正在尝试使用来自使用 NSJSONSerialization 制作的 JSON 字典的数据构建 UITableView。我不知道从哪里开始。我已经尝试了 3 天,但筒仓一无所获。这是我想出的代码:

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kJsonURL [NSURL URLWithString: @"http://rs-hosting.nl/panel/serverstatus_json.php"]

#import "ViewController.h"



@implementation ViewController

- (void)viewDidLoad
{    
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:kJsonURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData {
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSDictionary *clients = [json objectForKey:@"announcements"];
    NSArray *announcements = [clients objectForKey:@"announcement"];
    NSDictionary* announcement = [announcements objectAtIndex:1];

}
- (void)viewDidUnload {

    [super viewDidUnload];
}
@end

我能够在标签中的单个视图上显示标签中的内容,但我不知道如何将 json 中的所有数据添加到表中。有人可以治愈我吗?

SDK:5.1 OSX:10.7.3 XCODE:4.3.1

提前 Tnx

4

2 回答 2

1

您需要将数据存储在某处(将 NSDictionary 与整个 json 文件一起保存就可以了),然后添加一个 UITableView,将其设置为将其数据源指向您的类,然后按照概述实现UITableViewDataSource 方法。

UITableView 将在填充表格时从这些方法请求数据。唯一需要的两个方法是tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:因此从这两个开始,并在进行任何其他自定义之前获取显示在表格上的数据。

于 2012-03-21T20:48:43.290 回答
1

在 NSDictionary 中获取数据后,将其保存在 NSArray 中,其中填充了您要显示的 NSString 对象,然后调用函数 [Tableview reloadData];在你的 ViewDidLoad 功能和你的好去。

于 2012-04-10T13:54:37.250 回答