Segue – 索引0超出空数组的边界

下午好,

我在有一个TableViewController工作的最后一步,我有问题的DetailViewController。

在这里你可以find我的Segue代码:当我声明“[self.carMakes objectAtIndex:[myIndexPath row]]”时,我遇到了问题,因为我收到错误:“ [__NSArrayM objectAtIndex::index 0超出空数组边界'有search错误,它是我的数组,是空的。

如果这能帮助你find我做错事情的地方,我会在下面发布更多的代码,因为我不知道为什么数组是空的,因为它在TableVlewController中显示一切正常。

CarTablewViewController.m

@implementation CarTableViewController @synthesize carMakes = _carMakes; @synthesize carModels = _carModels; @synthesize carImages = _carImages; //@synthesize jsonArray = _jsonArray; - (void)viewDidLoad { [super viewDidLoad]; [self fetchJson]; [self.tableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [_jsonArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"carTableCell"; CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"id"]; cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"]; NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"]]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage * carPhoto = [UIImage imageWithData:imageData]; cell.carImage.image = carPhoto; return cell; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"ShowCarDetails"]) { CarDetailViewController *detailViewController = [segue destinationViewController]; NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow]; NSLog(@"TEST"); detailViewController.carDetailModel = [[NSMutableArray alloc] initWithObjects: [self.carMakes objectAtIndex:[myIndexPath row]], [self.carModels objectAtIndex:[myIndexPath row]], [self.carImages objectAtIndex:[myIndexPath row]], nil]; } } -(void)fetchJson { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSString * urlString = [NSString stringWithFormat:@"http://website.com/service.php"]; NSURL * url = [NSURL URLWithString:urlString]; NSData * data = [NSData dataWithContentsOfURL:url]; self.carModels = [[NSMutableArray alloc] init]; self.carMakes = [[NSMutableArray alloc] init]; self.carImages = [[NSMutableArray alloc] init]; @try { NSError *error; [_jsonArray removeAllObjects]; _jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error]; } @catch (NSException * e) { NSLog(@"Exception: %@", e); } @finally { [self.tableView reloadData]; } } ); } @end 

提前致谢。

你的tableview使用[_jsonArray objectAtIndex:indexPath.row]填充值。 在-(void)fetchJson你正在初始化数组,但不加载任何数据

所以当你这样做[self.carMakes objectAtIndex:[myIndexPath row]]该数组不包含任何东西

你的表视图是使用这个代码来获取制作和模型…

 cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"id"]; cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"]; 

但是你的塞格正在这样做

 [self.carMakes objectAtIndex:[myIndexPath row]], [self.carModels objectAtIndex:[myIndexPath row]], 

在你的提取你不填充self.carMakesself.carModels 。 填充它们或使用

 [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"], [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"user"], 

在你的Segue