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

标签 iphone ios objective-c json

您好,我正在尝试在表格 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/

相关文章:

ios - 如何在swift 3中设置Pan Gesture Limit

iphone - 标签栏界面方向问题

ios - TextField 和 Button 没有出现在容器 View 中?

iphone - 本地化的 UITabBarItem "Back"标题

IOS:如何仅使用点击手势即可允许触发的转场和发送的 Action

iphone - 我似乎无法在我的xcode版本上建立存档

iphone - 线程1:信号SIGABRT xcode 4.4 ios 5.1 Objective-c帮助>。<

ios - Xcode 8 - 自动布局问题

ios - 以编程方式生成的 Excel 和 Numbers CSV 文件格式问题

ios - Xcode 10 "Could not build module Darwin/Foundation/CoreFoundation"等