davidOhara
davidOhara

Reputation: 1008

Can't delete row in UITableView

Hi the following function doesn´t work for me and i don´t get why....

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source
        NSString *selectedCategory = [self.daten objectAtIndex:indexPath.row];
        [self.daten removeObjectAtIndex:indexPath.row]; //we assume, that mySourceArray is a NSMutableArray we use as our data source
        [self.tableView beginUpdates];

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tableView endUpdates];

        // ...
    }
}

I don´t have any Sections...I always get that exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (19) must be equal to the number of rows contained in that section before the update (19), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' * First throw call stack: (0x342692a3 0x33a3b97f 0x3426915d 0x3922e2af 0x36dd5b83 0x5453d 0x36f6b5d9 0x36e8c0ad 0x36e8c05f 0x36e8c03d 0x36e8b8f3 0x36e8c0ad 0x36e8c05f 0x36e8c03d 0x36e8b8f3 0x36e8bde9 0x36db45f9 0x36da1809 0x36da1123 0x34f195a3 0x34f191d3 0x3423e173 0x3423e117 0x3423cf99 0x341afebd 0x341afd49 0x34f182eb 0x36df5301 0x24343 0x38e34b20) libc++abi.dylib: terminate called throwing an exception

Upvotes: 1

Views: 871

Answers (1)

dreamzor
dreamzor

Reputation: 5925

You need to update your number of rows in numberOfRowsInSection: method. As i can see, you need to return your [self.daten count] in this method.

As its told, the system checks this number before/after update/delete to keep data integrity.

You always have a section :)

Implement it like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.daten count];
}

Also, remember that you need to set both UITableViewDelegate and UITableViewDataSource to self.

OK, the problem is: NSMutableArray's removeObjectAtIndex: method does not resize it. So you have to handle it by yourself. Create new array with right capacity and move the rest of elements to it. You can google how to resize NSMutableArray.

Upvotes: 2

Related Questions