1

データを解析して出力も表示できますが、アプリケーションがクラッシュする JSON を含むビューを呼び出すと、それらをテーブル ビューに表示できません。

これは私のコードです:

コードの最初の行では、バックグラウンド キューを返すマクロを定義します。そのための kBgQueue ショートカットがあるのが好きなので、コードをよりタイトに保つことができます。コードの 2 行目では、この URL http://api.kivaws.org/v1/loans/search.json?status=fundraisingを指す NSURL を返す kLatestKivaLoansURL という名前のマクロを作成します。

#define kBgQueue dispatch_get_global_queue(
 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString: 
 @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] //2
 @interface TeacherViewController ()
   @end
@implementation TeacherViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

バックグラウンド キューを提供するマクロとして kBGQueue を定義しましたか?

- (void)viewDidLoad

{
    [super viewDidLoad];

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

}
@synthesize latestLoans;
@synthesize arrayOfFighterName;

が呼び出され、NSData インスタンスが渡されます。この場合、JSON ファイルは比較的小さいので、メイン スレッドの fetchedData: 内で解析を行います。大規模な JSON フィードを解析している場合 (これはよくあることです)、必ずバックグラウンドで実行してください。

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1
                          options:kNilOptions
                          error:&error];

     latestLoans = [json objectForKey:@"loans"]; //2
    arrayOfFighterName=[[NSMutableArray alloc] init];
//NSLog(@"loans: %@", latestLoans); //3
    for( int i = 0; i<[latestLoans count]; i++){

        // NSLog(@"%@", [matchListArray objectAtIndex:i]);
        arrayOfFighterName[i]=[[latestLoans objectAtIndex:i] objectForKey:@"name"];
       // NSLog(@"%@", [arrayOfFighterName objectAtIndex:i]);
    }



}
- (void)viewDidUnload
{

    [super viewDidUnload];
    latestLoans = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;

}

結果をテーブル ビューで表示するが、残念ながら結果が表示されず、アプリがクラッシュする

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
   return [arrayOfFighterName count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                           dequeueReusableCellWithIdentifier:@"TeacherCell"];
            cell.textLabel.text = [arrayOfFighterName objectAtIndex:indexPath.row];
       return cell;


   //  NSLog(@"viewDidLoad is called");
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
4

1 に答える 1

1

取得したデータ メソッドを次のように変更します。

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1
                          options:kNilOptions
                          error:&error];

     latestLoans = [json objectForKey:@"loans"]; //2
    arrayOfFighterName=[[NSMutableArray alloc] init];
//NSLog(@"loans: %@", latestLoans); //3
    for( int i = 0; i<[latestLoans count]; i++){

        // NSLog(@"%@", [matchListArray objectAtIndex:i]);
        arrayOfFighterName[i]=[[latestLoans objectAtIndex:i] objectForKey:@"name"];
       // NSLog(@"%@", [arrayOfFighterName objectAtIndex:i]);
    }


[tableView reloadData];
}

最初は、データが配列に追加される前に tableView がロードされるためです。非同期呼び出しのため。データがサーバーから取得されると、データが解析されて配列に追加されます。したがって、データを表示するには、tableView をリロードする必要があります。

このチュートリアルの非同期呼び出しについて参照してください。

于 2012-11-04T10:11:53.120 に答える