0

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

2 回答 2

1

这个错误的原因是 that jsonResultsis not a NSArraybut a NSDictionary

编辑:正如@H2CO3 正确所说,json已经是NSArray您要使用的。用于[json objectAtIndex:indexPath.row]获取包含NSDictionary您想要在相应 中显示的所有详细信息tableViewCell

于 2013-04-05T14:51:26.817 回答
1

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

jsonResults = [json objectAtIndex:0];

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

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

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

于 2013-04-05T14:54:21.743 回答