简体   繁体   中英

NSRangeException Error with NSArray - While setting image view in another ViewController

I'm getting an image from the AppDelegate, then setting it to the ViewController's table.imageView property. It's throwing me an NSRangeException:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray     objectAtIndex:]: index (2) beyond bounds (2)'

I made sure my array and rows are counted by [array count]. Very confused. Here's the code:

#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;
}

It's probably this line:

cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];

The console output says there are 2 elements in the array. Elements start at 0; so you can access objectAtIndex:0 and objectAtIndex:1. Two would be the 3rd element of the array, and is out of bounds.

Sorry if that's all obvious, just taking a quick stab... Enjoy. :)

EDIT Actually, the issue could be that imageRead is nil when you add it to the array. That would cause the array to have only 2 elements in it. You may check for that, and/or use [NSNull null] if you don't have an image...

Make sure the array actually has 3 elements by doing something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM