gpt4 book ai didi

ios - TableView iOS 中的 JSON 数据已加载

转载 作者:行者123 更新时间:2023-11-29 13:25:37 27 4
gpt4 key购买 nike

我可以解析数据并查看输出,但我无法在表格 View 中显示它们当我调用包含 JSON 的 View 时,应用程序崩溃了

这是我的代码:

在第一行代码中,我们定义了一个宏,它为我们返回了一个后台队列——我喜欢为此使用 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;

}

在 TableView 中显示结果但不幸的是结果没有显示并且应用程序崩溃了

- (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

最佳答案

更改获取数据的方法,如:

- (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 .

关于ios - TableView iOS 中的 JSON 数据已加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com