gpt4 book ai didi

iphone - 在 UITableView 中显示带有 JSON 内容的 NSArray

转载 作者:行者123 更新时间:2023-11-28 22:36:28 24 4
gpt4 key购买 nike

您好,我正在尝试在表格 View 中显示来自 NSArray 的内容,但 NSString *place= [jsonResults objectAtIndex:indexPath.row]; 有问题我正在尝试增加 objectAtIndex,以便显示来自 json 的结果。以下是我收到的错误:

> *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary
> objectAtIndex:]: unrecognized selector sent to instance 0x714bda0'

我也尝试了以下方法,但我也遇到了错误:

NSDictionary *place= [jsonResults objectAtIndex:indexPath.row];

 #import "leagueTableViewController.h"

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

#define kjsonURL [NSURL URLWithString: @"http://api.statsfc.com/premier- league/table.json?key=o"]
@interface leagueTableViewController ()

@end

@implementation leagueTableViewController{

NSMutableArray *jsonResults;
}

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

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_async(kBgQueue, ^{

NSData* data = [NSData dataWithContentsOfURL:

kjsonURL];

[self performSelectorOnMainThread:@selector(fetchedData:)

withObject:data waitUntilDone:YES];

});
}




- (void)fetchedData:(NSData *)responseData {

NSError* error;

NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

jsonResults= [json objectAtIndex:0];
NSString *team = [jsonResults valueForKey:@"team"];

NSString *position = [jsonResults valueForKey:@"position"];

NSLog(@"team: %@", team); //3

NSLog(@"position: %@", position);

[self.tableView reloadData];



}


-(IBAction)btnBack:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];


}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath

{


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


NSString *place= [jsonResults objectAtIndex:indexPath.row];


NSString *position1 = [place valueForKey:@"position"];
NSString *won= [place valueForKey:@"won"];
NSString *lost= [place valueForKey:@"lost"];
NSString *played= [place valueForKey:@"played"];
NSString *points= [place valueForKey:@"points"];

cell.textLabel.text = [place valueForKey:@"team"];

cell.detailTextLabel.text = [NSString stringWithFormat:@"Position: %@ Played: %@ won: %@ lost: %@ Points: %@",position1,played,won,lost,points];



return cell;


}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

@end

最佳答案

您要么误解了返回的 JSON 的结构,要么没有执行您实际尝试执行的操作,或者两者兼而有之。在我看来你想要显示所有团队,所以

jsonResults = [json objectAtIndex:0];

是错误的,jsonResults应该直接是NSJSONSerialization方法的返回值:

jsonResults = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

(如果您不使用 ARC,请添加一个 retain,因为此方法返回一个自动释放的对象。)

关于iphone - 在 UITableView 中显示带有 JSON 内容的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836960/

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