0

I keep getting an uncaught exception error when there is no image to add from my image array. Here is some of my code:

This is my - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info Method:

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
self.photoImage = image;
[self.photoArray addObject:self.photoImage];
//[self.tableView reloadData];
[picker dismissViewControllerAnimated:YES completion:nil];

This is my TableView cell block:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];

XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Poiret One" size:18];
cell.textLabel.text = toDoItem.itemName;

NSDictionary *dict = [self.tableArray objectAtIndex:indexPath.row];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.text = [dict objectForKey:@"Time"];

//adding blank index and crashing
UIImage *photo = [self.photoArray objectAtIndex:indexPath.row];
    cell.imageView.image = photo;

if (toDoItem.completed) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;

I did initialize the array in the [View DidLoad] Method.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
vx2ko
  • 43
  • 3
  • 1
    What's the full and exact error message? Which line of code causes the crash? – rmaddy Aug 01 '14 at 16:59
  • How do you even trigger the insertion? I see reloadData commented out.. – Legoless Aug 01 '14 at 17:03
  • @rmaddy the fully error is "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'" – vx2ko Aug 01 '14 at 18:51
  • @rmaddy The crash occurs at "UIImage *photo = [self.photoArray objectAtIndex:indexPath.row];" – vx2ko Aug 01 '14 at 18:52
  • @Legoless it crashes regardless. I've got in another method for my addTask button. – vx2ko Aug 01 '14 at 18:55
  • I know it is trying to add an object that is not in that index. I guess the better question is how do I keep if from adding to the cell if there is no object? – vx2ko Aug 01 '14 at 19:43
  • Do you have any extra cell or cells at the top of the the tableView that is not used for photos? Like maybe a title or something. If so you'd need to offset indexPath.row (e.g. indexPath.row - 1 if you have one extra row up top used for a title) – rymagno Aug 01 '14 at 19:47
  • you can check that it is within the array bounds simply by doing if(indexPath.row<[self.photoArray count]) //add the photo – rymagno Aug 01 '14 at 19:49
  • Thank you @user2608440. That if statement is allowing it to add a blank cell without crashing now. Next issue. It is the image to the cell right after the previous cell. Is there a way to add a blank object when my addButton is pressed? – vx2ko Aug 01 '14 at 19:56

1 Answers1

0

To make sure you are within bounds you can do this:

if(indexPath.row<[self.photoArray count]) //add the photo

But, I would be concerned as to why you got that error in the first place. Is it because self.toDoItems, self.tableArray, and self.photoArray are different size arrays? If so then make sure to check their sizes too.

EDIT: For your second question in the comments, to add a blank row on addButton method, you would need to add an empty object to the datasource. I would assume you would have to:

[self.toDoItems addObject:<whatever your empty object looks like>];
[self.tableArray addObject:<whatever your empty object looks like>];
[self.tableView reloadData];

After the data is updated though, you'd have to replace the last cell (empty object) like so:

[self.toDoItems replaceObjectAtIndex:[self.toDoItems count]-1 withObject:newObject];
[self.tableArray replaceObjectAtIndex:[self.tableArray count]-1 withObject:newObject];
[self.tableView reloadData];
rymagno
  • 458
  • 2
  • 9
  • `self.toDoItems` and `self.tableArray` are both always the same size. Its my `self.photoArray` that is the one that changes size when the users wants to add a photo to the cell. This is causing the images to add to each cell after one another. – vx2ko Aug 01 '14 at 20:07
  • For the rows that don't have images you could blank images to the array for those rows (http://stackoverflow.com/questions/12235570/how-to-create-a-blank-transparent-png-in-objective-c) – rymagno Aug 01 '14 at 20:09
  • Sorry if my last comment was a bit confusing. `self.toDoItems` and `self.tableArray` stay the same size. `self.toDoItems` adds a users tasks to the `UITableViewCell` and `self.tableArray` adds a timestamp to the same cell. A user will have an option to add a photo the a new task/cell. The problem is the images added do not populate in the correct cell. They populate the cell corresponding to its index in the `self.photoArray`. – vx2ko Aug 01 '14 at 21:36
  • You need to make sure that self.photoArray is the same size as the other arrays. Every time you addObject to the other arrays, you need to addObject to photoArray. If there is no photo selected for that record, add a blank photo like mentioned in my last comment. It will look like there is no photo in the cell, but it is actually a transparent image. – rymagno Aug 01 '14 at 22:09