I am trying to add a row with an insert (green plus) control to my table view when the user clicks on the edit. So far I have an insert line to show, but if the user tries to scroll through the table view when he is in edit mode, the application crashes with the following error:
* Application termination due to an uncaught exception 'NSRangeException', reason: '* - [_ PFArray objectAtIndex:]: index (9) outside (9)'
I know that a similar question was asked earlier , but since I'm new to programming, I may need a little more money to hold hands. The answer to this question suggested making sure that numberOfRowsInSection is being updated properly. I think this is mine, but I'm obviously wrong about something.
This is what I got so far in the table view controller, which is the root view controller of my UINavigation controller. At the moment, this is just a dummy table - nothing is connected 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];
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];
}
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
Thank!
source
share