2013-04-05 75 views
0

嗨,我想显示内容从NSArray在表视图中,但有NSString *place= [jsonResults objectAtIndex:indexPath.row];问题我想增加objectAtIndex,以便从JSON结果显示。下面是我收到的错误:用UITableView中的JSON内容显示NSArray

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

我也曾尝试用也以下,但我得到的错误:

的NSDictionary *地点= [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 

回答

1

你要么误解了返回的JSON的结构,要么你不去做你实际想要做的事情,或者两者兼而有之。在我看来,要显示所有的球队,所以

jsonResults = [json objectAtIndex:0]; 

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

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

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

+0

谢谢你,因为它showe d解决了问题! – paulpwr 2013-04-05 15:07:20

1

这样做的原因错误是jsonResults不是NSArray而是NSDictionary

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

+0

相当新的ios任何方向,我需要改变将是有帮助的 – paulpwr 2013-04-05 14:59:12