您好我正在尝试在表格视图中显示来自 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