Why does my application crash when I add an insert row in my table view?

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];

    // 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

Thank!

+3
source share
2 answers

parts of your method tableView:cellForRowAtIndexPath:are incorrect

, tableview . 10 _trips.

, 11 :

if (tableView.editing) rows++;

tableview 10 :

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

10.

, . ,

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

btw. if (indexPath.row == !0) - , if (indexPath.row != 0)

+3

, , , . Pls . ,

0

Source: https://habr.com/ru/post/1795476/


All Articles