Vladyslav  Kudelia
Vladyslav Kudelia

Reputation: 33

I have a issue " index 3 beyond bounds [0 .. 2]' "

#import "MasterTableViewController.h"

@interface MasterTableViewController ()

@end

@implementation MasterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self isEditing] ? self.wishListItems.count + 1 : self.wishListItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    WishListItem *item = self.wishListItems[indexPath.row];

    if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
        cell.textLabel.text = @"New subject";
        cell.imageView.image = nil;
    } else {
        cell.textLabel.text = item.name;
        cell.imageView.image = item.photo;
    }
    return cell;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    } else {
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.wishListItems.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.wishListItems removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        WishListItem *newItem = [[WishListItem alloc] initWithName:@"New subject" photo:nil price:0.0 andNotes:@"Empty"];
        [self.wishListItems insertObject:newItem atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row >= [self.wishListItems count]) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [self.tableView deselectRowAtIndexPath:indexPath animated:true];
//    if (indexPath.row >= self.wishListItems.count && self.editing) {
//        [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath];
//    }
//}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        WishListItem *item = self.wishListItems[indexPath.row];
        DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
        detailViewController.item = item;
    }
}


@end

I changed "number of row in section" and inserted new row but issue " index 3 beyond bounds [0 .. 2] ", and I changed "number of row in section" without increment, appear issue " reason: 'attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update' " I double-check a thousand times and still can not find the problem, help who was able to find what is the problem in the code.

Upvotes: 2

Views: 1119

Answers (1)

dan
dan

Reputation: 9825

In cellForRowAtIndexPath:, you're accessing your array before you do your bounds check, so it will crash when your table is editing.

Only do the array access after the bounds check:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (indexPath.row >= [self.wishListItems count] && self.tableView.isEditing) {
        cell.textLabel.text = @"New subject";
        cell.imageView.image = nil;
    } else {
        WishListItem *item = self.wishListItems[indexPath.row];
        cell.textLabel.text = item.name;
        cell.imageView.image = item.photo;
    }
    return cell;
}

Upvotes: 1

Related Questions