我可以解析数据并查看输出,但我无法在表格视图中显示它们 当我调用包含JSON的视图时,应用程序崩溃了
这是我的代码:
在第一行代码中,我们定义了一个宏,它为我们提供了一个后台队列 - 我喜欢有一个kBgQueue快捷方式,所以我可以保持我的代码更紧密。 在第二行代码中,我们创建了一个名为kLatestKivaLoansURL的宏,它返回一个指向此URL 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 :(得分: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中的异步调用。