Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSArray出现NSRangeException错误-在另一个ViewController中设置图像视图时_Iphone_Objective C_Xcode - Fatal编程技术网

Iphone NSArray出现NSRangeException错误-在另一个ViewController中设置图像视图时

Iphone NSArray出现NSRangeException错误-在另一个ViewController中设置图像视图时,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我从AppDelegate获取图像,然后将其设置为ViewController的table.imageView属性。这给我带来了一个异常: *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (2)' 我确保我的数组和行按[array count]计数。非常困惑。代码如下: #pr

我从AppDelegate获取图像,然后将其设置为ViewController的table.imageView属性。这给我带来了一个异常:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray     objectAtIndex:]: index (2) beyond bounds (2)'
我确保我的数组和行按[array count]计数。非常困惑。代码如下:

#pragma mark - viewWillAppear
- (void)viewWillAppear:(BOOL)animated {

PBAppDelegate *dataObject = (PBAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *titleRead = dataObject.title;
NSString *descRead = dataObject.desc;
UIImage *imageRead = dataObject.image;


if ([titleRead isEqualToString:@"" ] || [descRead isEqualToString:@""]) {
    // it's nil
} else {
    if (titleRead) {
        [data addObject:[NSArray arrayWithObjects:titleRead, descRead, imageRead, nil]];
        dataObject.title = @"";
        dataObject.desc = @"";
        [tableView reloadData];
    }

    NSUserDefaults *dataDefaults = [NSUserDefaults standardUserDefaults];
    [dataDefaults setObject:[NSArray arrayWithArray:data] forKey:@"dataArrayKey"];
    [dataDefaults synchronize];
}

}

#pragma mark - viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
data = [[NSMutableArray alloc] init];

self.data = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataArrayKey"];
[tableView reloadData];
}

#pragma mark - Table Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {

static NSString *cellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:0];
cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:1];
cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

可能是这一行:

cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
控制台输出显示数组中有2个元素。元素从0开始;因此,您可以访问objectAtIndex:0和objectAtIndex:1。2将是数组的第三个元素,超出范围

抱歉,如果这都是显而易见的,只是快速尝试一下。。。享受。:)

编辑
实际上,问题可能是将
imageRead
添加到数组中时,它是零。这将导致数组中只有2个元素。如果没有图像,您可以进行检查和/或使用[NSNull null]。

执行以下操作以确保数组实际包含3个元素:

NSArray *array = [data objectAtIndex:indexPath.row];
if ([array count] > 2)
{
 cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:2]];
}

在代码中,有一个数组。0=标题,1=描述,3=图像。您将它们放在这里:[data addObject:[NSArray arrayWithObjects:titleRead,descRead,imageRead,nil]]。我的理论是插入“imageRead”时为零。这意味着您正在执行此操作[data addObject:[NSArray arrayWithObjects:titleRead、descRead、nil、nil]]——将数组1元素提前结束;在尝试提取数据时导致异常。你应该检查nil,也许用[NSNull-null]替换它,它会给你一个“null对象”,你可以把它塞进你的数组中。我个人建议你把我们的内部数组,把它变成一个简单的对象(product.m或其他什么),带有title、desc和image的属性。然后创建一个数组。您的代码将更具可读性,几乎可以肯定这种崩溃会消失。