-1

Here is my problem :

I have an NSMutableArray *notes and which the source of my UITableView* _gradesTV.

I added the method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath here :

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"%ld", (long)indexPath.section);
        NSLog(@"%@", notes);




        [_gradesTV beginUpdates];

        [notes removeObjectAtIndex:indexPath.section];

        [_gradesTV deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                 withRowAnimation:UITableViewRowAnimationFade];

        [_gradesTV endUpdates];




        NSLog(@"%@", notes);
    }

[_gradesTV reloadData];

}

But when i delete an element I get this error :

*** Terminating app due to uncaught exception 'NSRangeException',
 reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

But the NSLog I make on the MutableArray show me 3 elements ...

The line removeObjectAtIndex on the notes (MutableArray) makes my app crash like it was executed twice ...

Thank you for your help ...

Github Link

if u want to pull the project and try ...

You will have to create grades with the + button then you can try to delete an item on my table view.

When adding a grade the first TextField is a string, the second one is a double value, the third one is also a double value.

valbu17
  • 4,034
  • 3
  • 30
  • 41
H. Wolber
  • 23
  • 1
  • 7

4 Answers4

0

Try with following code..

- (NSInteger)numberOfSections {
    return [notes count];
}

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"%ld", (long)indexPath.section);
        NSLog(@"%@", notes);
        [notes removeObjectAtIndex:indexPath.section];
        NSLog(@"%@", notes);
    }

[_gradesTV reloadData];

}
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
0

Firstly remove UITableView's beginUpdates and endUpdates

Secondly remove UITableView's reloadData as no need when using UITableViewCellEditingStyleDelete and UITableViewCellEditingStyleInsert

Now your code will be :

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"Current Section : %ld", (long)indexPath.section);
        NSLog(@"Before : %@", notes);

        [notes removeObjectAtIndex:indexPath.section];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        id sectionDataSource = [notes objectAtIndex:indexPath.section];
        if ([sectionDataSource count] > 0)
        {
            // Section is not yet empty, so delete only the current row.
            [_gradesTV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            // Section is now completely empty, so delete the entire section.
            [_gradesTV deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                     withRowAnimation:UITableViewRowAnimationFade];
        }

        NSLog(@"After : %@", notes);
    }
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • IMO I think major problem is at `indexPath.section`. OP is referring to section and not row... – Fahim Parkar Feb 18 '15 at 13:50
  • I already tryed this code i found on stackoverflow but it didn't solve my problem, i changed the question and included a github link with the whole project if u wanna try – H. Wolber Feb 18 '15 at 14:50
0

The problem is you are deleting section instead of row.

[notes removeObjectAtIndex:indexPath.section]; should be [notes removeObjectAtIndex:indexPath.row];

Also [_gradesTV deleteSections should be [_gradesTV deleteRow...

Note: You should use [_gradesTV deleteRow... or [_gradesTV reloadData]; because both are useless. I would say just use [_gradesTV deleteRow...

Also not sure beginUpdates & endUpdates are for what. Remove the un-necessary stuff.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • My project work more with sections than rows and if i use `indexPath.row` i won't delete the right item ... I changed the question to include the Github link of the project if you want to try with all files – H. Wolber Feb 18 '15 at 14:52
0

Ok I figured from where came my mistake, it was just that I didn't used the same MutableArray to get the number of sections ...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [notes count];
}

The old method had an old NSMutableArray I was using to test my code ...

Thank you all even if I was just too noob to notice it by myself ....

H. Wolber
  • 23
  • 1
  • 7