0

我有一个来自 JSON 文件的解析数据。它在我的 UITableView 的第一个视图中就像一个魅力。但是,当我点击一个项目时,它会显示一个空白的第二个视图。

主视图.h

@interface MasterViewController : UITableViewController
{
    NSArray *json;
}

@property (nonatomic, retain) NSArray *json;

@end

主视图.m

#import "MasterViewController.h"
#import "InformationViewController.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

@synthesize json;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://j4hm.t15.org/ios/console.php"]];
    [self performSelectorOnMainThread: @selector(fetchedData:) withObject: jsonData waitUntilDone: YES];
}

- (void)fetchedData:(NSData *)responseData
{
    NSError *error;

    self.json = [NSJSONSerialization JSONObjectWithData: responseData options: kNilOptions error: &error];

    NSLog(@"String is %@", self.json);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [json count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = [[json objectAtIndex: indexPath.row] objectForKey: @"Console"];

    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationViewController *informationViewController = [[InformationViewController alloc] initWithStyle:UITableViewStylePlain];

    informationViewController.title  = [[json objectAtIndex: indexPath.row] objectForKey: @"Console"];
    informationViewController.Information = [[json objectAtIndex: indexPath.row] objectForKey: @"Model"];

    NSLog(@"String is %@", informationViewController.Information);

    [self.navigationController pushViewController: informationViewController animated:YES];

}

信息视图.h

@interface InformationViewController : UITableViewController

@property (nonatomic, strong) NSArray * Information;

@end

信息视图.m

@interface InformationViewController ()

@end

@implementation InformationViewController

@synthesize Information;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    cell.textLabel.text = [[Information objectAtIndex: indexPath.row] objectForKey: @"Model"];

    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}
4

3 回答 3

1

在这里,我发布了另一个答案,只是因为我不想引起混乱。

项目代码: http: //www.cogzentappz.com/demo/iostutorial/TestJson.zip

而不是传递选定的值,而是传递整个ArrayNSUsedDefaults使用以下代码检索它们。

  - (void)fetchedData:(NSData *)responseData
    {
        NSError *error;

        self.json = [NSJSONSerialization JSONObjectWithData: responseData options: kNilOptions error: &error];

        NSLog(@"String is %@", self.json);

     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray *arrayObj = [[NSMutableArray alloc] init];


        for(int i = 0 ; i<[self.json count] ; i++) {
            [arrayObj addObject:[json objectAtIndex:i]]];
        }

        [standardDefaults setObject:arrayObj forKey:@"longArray"];
        [arrayObj release];


    }

在下一个视图中,您可以使用以下方法检索数据。在ViewDidLoad使用informationViewcontroller这个

//reading

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayObj = [standardDefaults objectForKey:@"longArray"];


for(int i = 0 ; i<[arrayObj count] ; i++) {
        NSLog(@"String is %@",[arrayObj objectAtIndex:i]);
    }

根据您的链接,Json 输出如下所示:

NSArray-->NSDictionary-->NSArray-->NSDictionary-->NSArray-->NSDictionary

转到链接: http: //json.bloople.net并发布所有 json 输出并按回车键。

因此,您必须根据该格式获取值。

for(int i = 0 ; i<[self.json count] ; i++) {
 NSArray *info_Array=[[self.json objectAtIndex:i ]valueForKey:@"Information"];
    NSLog(@"info_Array is %@", info_Array);

    for(int j = 0 ; j<[info_Array count] ; j++)
    {

        NSLog(@"Model is %@", [[info_Array objectAtIndex:j ]valueForKey:@"Model"]);
        NSArray *title_Array=[[info_Array objectAtIndex:j ]valueForKey:@"Title"];
        for(int k = 0 ; k<[title_Array count] ; k++)
        {
            NSLog(@"Game is %@", [[title_Array objectAtIndex:k ]valueForKey:@"Game"]);
            NSLog(@"Publisher is %@", [[title_Array objectAtIndex:k ]valueForKey:@"Publisher"]);
        }

    }

    NSString *Console_string= [[dataArray objectAtIndex:i ]valueForKey:@"Console"];
    NSLog(@"String is %@", Console_string);

}

NSString *Console_string= [[self.Json objectAtIndex:i ]valueForKey:@"Console"];
NSLog(@"String is %@", Console_string);
}
于 2013-01-06T10:02:45.037 回答
0

用这个改变你的 MaterView.m 代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InformationViewController *informationViewController = [[InformationViewController alloc] initWithStyle:UITableViewStylePlain];
informationViewController.title  = [[json objectAtIndex: indexPath.row] objectForKey: @"Console"];
informationViewController.Information = [[json objectAtIndex: indexPath.row] objectForKey: @"Model"];
NSLog(@"String is %@", informationViewController.Information);
[self.navigationController pushViewController: informationViewController animated:YES];
}

希望对你有帮助...

于 2013-01-05T11:43:04.933 回答
0

我认为您必须在获取数据后重新加载数据。现在,您指定NSArrray *json为空,因为计数返回 0。因此,您禁止填充表视图。[tableView reloadData];获取数据后调用。

于 2013-01-05T11:53:29.587 回答