I use UIActionSheet to confirm for deleting row in TableView. I have 3 rows but when I swap to delete first row, the first row not delete but the third row is deleted. After I continue to delete first row again but second row is deleted. I don’t know why. Please help me to resolve this small problem.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Delete" otherButtonTitles: nil];
[actionSheet showInView:self.view];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
[listOfName removeObjectAtIndex:self.selectedIndex];
//selectedIndex = indexPath.row;
[self viewWillAppear:YES];
}
}
Here is the problem
The value of
selectedIndexof yourUIActionSheetis always 0 since that is not set to the index of table row to be deleted.Set it’s value to the
indexPath.rowbefore presenting theUIActionSheet.Then in
clickedButtonAtIndex:method within thecase0, make sure you reload the table view.