3

Possible Duplicate:
change Table to Edit mode and delete Rows inside a normal ViewController

I want to delete selected row from tableView. I want to provide the functionality to the user to delete the row when he slips or flicks his finger on the row. I know the editing style which provides a circular red button with -ve sign on it.

I have tried this code:

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


[tableView beginUpdates];    
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Do whatever data deletion you need to do...
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];      
}       
[tableView endUpdates];
[tableView reloadData];
}

I get the delete button but when I click it SIGABRT Error is there.

Community
  • 1
  • 1
Dinesh_
  • 259
  • 1
  • 5
  • 9
  • You can check my answer Here : http://stackoverflow.com/questions/14030944/how-can-i-delete-tableview-row-from-detail-view/14031052#14031052 – Bhavin Jan 07 '13 at 07:33

5 Answers5

17

Try like below lines:

[bookmarks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

bookmarks is my array name that I load on table view cell you should give your array name in place of this.

Vishal
  • 8,246
  • 6
  • 37
  • 52
  • its work but when i add another data all deleted data are show again ? – Dinesh_ Jan 07 '13 at 07:29
  • in end again select that array again means to say reload your data of array using select that array again in end... – Vishal Jan 07 '13 at 07:34
  • As in my case I am again give array to NSUserDefault for relaoad in end like: [[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"]; – Vishal Jan 07 '13 at 07:36
  • @Vishal i give this answer but late and wrong about that , but you solve it very well and easily.. keep it up dude:) – Paras Joshi Jan 09 '13 at 09:34
  • suppose i have cell0 ,cell1 , cell2 I delete cell1 , then in tableview i have shown cell0 and cell1 only Now again if i delete cell1 then it kills ? explain – 9to5ios Oct 13 '16 at 09:25
  • @iphonemaclover: After delete row and data from Array you have to reload your data Array and then check... – Vishal Oct 13 '16 at 11:35
3

After removing the table row delete the corresponding index dataSource from your dataSource to table.and then reload table again.

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 [dataArray removeObjectAtIndex:indexPath.row];
  [tableView reloadData];
Madhu
  • 994
  • 1
  • 12
  • 33
3

just replace the Code

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle==UITableViewCellEditingStyleDelete)
    { 
        [dataArray removeObjectAtIndex:indexPath.row];
    }


    [tableView reloadData];
}
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
0

Try this

 [tableArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
ask4asif
  • 676
  • 4
  • 10
0

Must Try This....

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 
user1673099
  • 3,293
  • 7
  • 26
  • 57