Segue - 索引 0 超出空数组的界限 [英] Segue - index 0 beyond bounds for empty array

查看:27
本文介绍了Segue - 索引 0 超出空数组的界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

我正在使用 TableViewController 的最后一步,但我在使用 DetailViewController 时遇到了问题.

I'm in the last step of having a TableViewController working and I'm having problems with the DetailViewController.

在这里您可以找到我的 Segue 代码:我在声明[self.carMakes objectAtIndex:[myIndexPath row]]"时遇到了问题,因为我收到了错误:[__NSArrayM objectAtIndex:]: index 0 Beyond the bounds for空数组'",我搜索了错误,结果显示我的数组是空的.

Here you find my Segue code: I have problems when declaring the "[self.carMakes objectAtIndex:[myIndexPath row]]" because I recieve the error: "[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'" and I have search the error and it's realated my array, that is empty.

如果这有助于您找到我做错的地方,我将在下面发布更多代码,因为我不知道为什么它是空的数组,因为它在 TableVlewController 中显示一切正常.

I'm going to post below more code if that helps you to find where I'm doing things wrong because I don't know why the array it's empty, because it's showing everything fine in the 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 中,您正在初始化数组但未使用任何数据加载它们

Your tableview is using [_jsonArray objectAtIndex:indexPath.row] to populate values. In -(void)fetchJson you are initialising the arrays but not loading them with any data

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

So when you do this [self.carMakes objectAtIndex:[myIndexPath row]] that array doesn't contain anything

您的表视图正在使用此代码获取品牌和型号...

Your table view is using this code to get make and model...

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

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

但是你的 Segue 正在这样做

But your Segue is doing this

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

在您的 fetch 中,您没有填充 self.carMakesself.carModels.填充它们或使用

In your fetch you are not populating self.carMakes or self.carModels. Either populate them or use

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

在你的转场

这篇关于Segue - 索引 0 超出空数组的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆