I am trying to add a row with an insert control (green plus) to my table view when the user presses edit. So far, I’ve got the insert row to show, but if the user tries to scroll the table view while it is in edit mode, the app crashes with the following error:
* Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[_PFArray objectAtIndex:]: index (9) beyond bounds (9)’
I know that a similar question has been asked before, but as I am new to programming I might need a bit more handholding. The answer to that question suggested making sure that numberOfRowsInSection was updating properly. I think mine is, but I am obviously making a mistake somewhere.
This is what I’ve got so far in my table view controller, which is the root view controller for my UINavigation Controller. At the moment it’s just a dummy table – nothing is hooked up except the edit button.
#import "RootViewController.h"
#import "AppDelegate_iPhone.h"
#import "Trip.h"
@implementation RootViewController
@synthesize trips = _trips;
@synthesize context = _context;
- (id)init
{
self = [super init] ;
if (self)
{
automaticEditControlsDidShow = NO;
}
return self ;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Trip" inManagedObjectContext:_context]];
NSError *error;
self.trips = [_context executeFetchRequest:fetchRequest error:&error];
self.title = @"Trips";
[fetchRequest release];
// Display an Edit button for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int rows = [_trips count];
if (tableView.editing) rows++;
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
Trip *info = [_trips objectAtIndex:indexPath.row];
if (tableView.editing)
{
if (indexPath.row == 0)
cell.textLabel.text = @"Add New Trip";
if (indexPath.row == !0)
cell.textLabel.text = info.tripName;
}
else
{
cell.textLabel.text = info.tripName;
}
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
if (self.editing && row == 0) {
if (automaticEditControlsDidShow)
return UITableViewCellEditingStyleInsert;
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
automaticEditControlsDidShow = NO;
[super setEditing:editing animated:animated];
NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0],nil];
[self.tableView beginUpdates];
if (editing) {
automaticEditControlsDidShow = YES;
[self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
} else {
[self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
}
[self.tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[_trips release];
[_context release];
[super dealloc];
}
@end
Thanks!
parts of your
tableView:cellForRowAtIndexPath:method are wrongImagine your tableview is in editingmode. and you have 10 objects in _trips.
You tell the tableview that you have 11 rows in the array:
And the tableview will try to access element with index 10 here:
But you don’t have an element with index 10. So you’ll get an exception
You have to change the logic that gives you the index in the array. Maybe like this
and btw.
if (indexPath.row == !0)does something different thanif (indexPath.row != 0)