2012-11-04 63 views
1

我可以分析数据,看输出也,但我不能在表视图 显示他们当我调用包含JSON应用程序崩溃JSON数据表视图的iOS加载

这是视图我代码:

在第一行代码,我们定义一个宏,让我们回到后台队列 - 我喜欢有一个kBgQueue快捷方式这一点,所以我可以保持我的代码更紧。 在代码的第二行,我们创建了一个名为kLatestKivaLoansURL宏返回我们指向此网址http://api.kivaws.org/v1/loans/search.json?status=fundraising的NSURL。

#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 
+0

http://emclstcd.tk – 2012-11-04 09:05:27

+0

apllication载入,但它给了我空表没有任何污染 –

+0

如果你NSLog(@“%@”,arrayOfFighterName);',它显示什么? – 2012-11-04 09:33:00

回答

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来显示数据。

请参阅有关此tutorial异步调用。