Rafay Hasan
Rafay Hasan

Reputation: 115

Delete a tableview cell by tapping the red minus icon in iOS

I want to delete the tableview cell right away without the confirmation of red "Delete" button press when user clicks the red minus icon. Steps 1. Hit edit button > red minus icon appears > hit the minus icon >> row deleted without showing the red "Delete" icon.

Upvotes: 0

Views: 1271

Answers (2)

User31
User31

Reputation: 279

This does the work you want.

- (nullable NSString *)tableView:(UITableView *)tableViews titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 
{
[arrOutputRecords removeObjectAtIndex:indexPath.row];

[tableViews deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                  withRowAnimation:YES];

[_tableView reloadData];

return @"";
}

Upvotes: 1

iAnurag
iAnurag

Reputation: 9346

Add these methods in your class:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if (self.tableView.editing) {
            return UITableViewCellEditingStyle.Delete;
    }

    return UITableViewCellEditingStyle.None;
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {

let alertController = UIAlertController(title: "Alert", message: "Are you sure?", preferredStyle:UIAlertControllerStyle.Alert)

 alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
   { action -> Void in
     arr.removeAtIndex(indexPath.row)
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
   })
   self.presentViewController(alertController, animated: true, completion: nil)

        }
    }

Upvotes: 0

Related Questions