0

I wanna delete a particular row while I drag it. Here is part of code.

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

    [tableData removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
Panky
  • 21
  • 4

3 Answers3

0
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
         [tableData removeObjectAtIndex:indexPath.row];
         [tableView reloadData];
   }

}
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
0

Do you set delegate ?

self.tableView.delegate = self;

With corresponding How do I get the delete button to show when swiping on a UITableViewCell?

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // delete from model
        // [self.tableView reloadData]
    }    
}

You can put on breakpoints into your code for check call functions

Community
  • 1
  • 1
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
0

yagnesh dobariya's answer is a working solution, and I would like to propose a modification that makes it more efficient.

Since you are only deleting one row at a time, you can keep using -deleteRowsAtIndexPaths:withRowAnimation: as in your original code. Just sandwich it between -beginUpdates and -endUpdates.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
         [tableView beginUpdates];

         [tableData removeObjectAtIndex:indexPath.row];
         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

         [tableView endUpdates];
   }

}
jperl
  • 1,066
  • 7
  • 14