2013-04-05 43 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