Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:23:56+00:00 2026-05-20T09:23:56+00:00

I am trying to add a row with an insert control (green plus) to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:23:57+00:00Added an answer on May 20, 2026 at 9:23 am

    parts of your tableView:cellForRowAtIndexPath: method are wrong

    Imagine your tableview is in editingmode. and you have 10 objects in _trips.

    You tell the tableview that you have 11 rows in the array:

    if (tableView.editing) rows++;
    

    And the tableview will try to access element with index 10 here:

    Trip *info = [_trips objectAtIndex:indexPath.row];
    

    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

    if (tableView.editing) 
    {   // tableview row count is _trips count + 1
        if (indexPath.row == 0)
            cell.textLabel.text = @"Add New Trip";
        if (indexPath.row != 0) {
            // realIndex = table view index - 1 
            Trip *info = [_trips objectAtIndex:indexPath.row - 1];
            cell.textLabel.text = info.tripName;
        }
    }
    else
    {
        Trip *info = [_trips objectAtIndex:indexPath.row];
        cell.textLabel.text = info.tripName;
    }
    

    and btw. if (indexPath.row == !0) does something different than if (indexPath.row != 0)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to delete a row from my table view and so far have
I'm trying to add a row to an InnoDB table with a simply query:
I am trying to add a row to a table in the database using
I'm trying to alter a table to add a new column, then insert a
I'm trying to insert another table row into a table after calling from a
I'm trying to use jQuery to add dynamic Add/Remove row function, but I meet
I'm trying to add a row to an existing spreadsheet using list feed. But
I am trying to insert rows into an html table with javascript. I have
net and I'm trying to insert a row into an entity, and I keep
I am trying to insert 2 rows into the same table. The first will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.