NSMutableArray removeObjectAtIndex: indexPath.row failed

I have a UITableViewController with an NSMutableArray in the header, like this:

NSMutableArray *someArray;

I also have a declared property:

@property (nonatomic, retain) NSMutableArray *someArray;

In the .m file, I load the array into a method:

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

    someArray = [[NSMutableArray alloc] init];

    [self loadArrayData];

    [tableview reloadData];
}

The table fills perfectly, adding is not a problem, but when I try to delete a row, the application crashes in the following row:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    [someArray removeObjectAtIndex:indexPath.row];

    //update table etc..

If I use a debugger, I see that the array has some objects, and when I register indexPath.row, I get a value that is inside the size of the array.

I don’t understand why it collapses on this line ... Who can help me?

Console Outputs:

* - "NSInvalidArgumentException", : '- [__ NSArrayI removeObjectAtIndex:]: , 0x6193890'

+3
3

, CoreData :

self.allContacts = (NSMutableArray*)[cdc.managedObjectContext executeFetchRequest:fetchRequest error:&error];

CoreDataController NSMutableArray.

, "mutableCopy" .

self.allContacts = (NSMutableArray*)[[cdc.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];

, (MutableArray *) , .

, .

+2

. :

NSMutableArray * tableViewController.m.

if (editingStyle == UITableViewCellEditingStyleDelete) {
   globalArray = [[NSMutableArray alloc] initWithArray:someArray];
   [globalArray removeObjectAtIndex:[indexPath row]]; 
}

globalArray someArray reloadData. .

+1
  • Not sure if this caused one of your problems, but if you have a property for someArray, use self.someArrayinstead someArray. Of course, after using self, you should automatically release it as follows:

self.someArray = [[[NSMutableArray alloc] init] autorelease];

(call it inside your viewController init)

then try:

[self.someArray removeObjectAtIndex:indexPath.row];

2. Try calling [self loadArrayData];inside viewDidLoad. It must be called before cellForRowAtIndexPath, and you will not need to callreloadData

if that still doesn’t help, then post the code loadArrayData

0
source

All Articles