【问题标题】:Segue - index 0 beyond bounds for empty arraySegue - 索引 0 超出空数组的范围
【发布时间】:2014-11-08 14:57:30
【问题描述】:

下午好,

我正处于 TableViewController 工作的最后一步,但我遇到了 DetailViewController 的问题。

在这里您可以找到我的 Segue 代码:我在声明“[self.carMakes objectAtIndex:[myIndexPath row]]”时遇到问题,因为我收到错误消息:“[__NSArrayM objectAtIndex:]: index 0 beyond bounds for空数组'" 并且我已经搜索了错误并且它已经实现了我的数组,它是空的。

如果可以帮助您找到我做错的地方,我将在下面发布更多代码,因为我不知道为什么数组是空的,因为它在 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

提前致谢。

【问题讨论】:

    标签: ios objective-c xcode xcode6


    【解决方案1】:

    您的 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"];
    

    但是你的 Segue 正在这样做

    [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"],
    

    在你的转场

    【讨论】:

    • 我的 jsonArray 填充了数据并且 TableViewController 显示数据正常,但是当我尝试将数据发送到我的 Segue 时不起作用。我必须如何编写那行代码才能将数组发送到我的 Segue?谢谢@Flexicoder
    • 感谢@Flexicoder,它适用于“CHAR”值,但是当涉及到图像时,它变得越来越难,因为我已经存储了图像的完整路径 URL。你知道我怎么能用图像做到这一点吗?再次感谢。
    • 我会将 URL 传递给 detailViewController 并让它下载
    • 好的,我将搜索更多相关信息,因为我从未做过类似的事情。你能给我一个线索吗?再次感谢@Flexicoder
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多