0

when I call the indexpath of cellForRowAtIndexPath: for deleting the cell it deletes de cell before the selected one... Heres how It works:

I add info to the tableview:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.tasks insertObject:textField.text atIndex:0];
    [self.userdefaults setObject:self.tasks forKey:@"tasks"];
    [self.userdefaults synchronize];

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(0) inSection:0];
    [self.tableview beginUpdates];
    [self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [self.userdefaults synchronize];
    [self.tableview endUpdates];
}

And then I edit the info with some gestures from a CocoaControl (MCSwipeTableViewCell):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//This is for deleting a cell
[cell setSwipeGestureWithView:cross color:redColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {


        [tableView beginUpdates];

        I tried to use this index and didnt work-->//NSIndexPath *myindex = [self.tableview indexPathForSelectedRow];

        [self.tasks removeObjectAtIndex:[self keyForIndexPath:indexPath].row];
        [self.userdefaults setObject:self.tasks forKey:@"tasks"];
        [self.userdefaults synchronize];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [tableView endUpdates];

        NSLog(@"Did swipe \"Cross\" cell");
    }];


}

- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath class] == [NSIndexPath class]) {
        return indexPath;
    }
    return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}

So when I try to delete It deletes a cell before the one it was selected, I need someones help, thanks!

Karlo A. López
  • 2,548
  • 3
  • 29
  • 56

2 Answers2

0

You can use [tableView indexPathForCell:cell] in your completion block to get the appropriate indexPath.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
-2

Try using: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; instead?

Joey Clover
  • 756
  • 5
  • 14