George
George

Reputation: 205

Have a UITableViewCell show the delete button without the red minus

I want to implement a UITableView that when I press a button in my ViewController, I can see the delete button on the right of the cell, but not the red minus circle on the the left of the cell.

Any ideas on this?

Upvotes: 7

Views: 3748

Answers (5)

viral
viral

Reputation: 4208

Just an approach:

  1. Have a delete button on a view and assign a method to it
  2. In this method, toggle a BOOL isDeleteEnabled (or whatever you name it) to TRUE
  3. Reload the UITableView and in cellForRowAtIndexPath check for that BOOL value
  4. If TRUE then load the all the UITableViewCell as custom cells with delete button on it (You need to set the selector method for deletion of a row)
  5. Delete as many cells as you want by tapping that button on the cells
  6. After deleting your choices you can again press the main button and toggle the value for isDeleteEnabled
  7. Again reload UITableView and this time in cellForRowAtIndexPath check the value of isDeleteEnabled - which will be FALSE. So, you need to load normal(or your original) UITableViewCells.

I have done the very similar thing following this approach.

Also, I haven't posted the code for it for three reasons:

  • It would be too lengthy in size
  • The code his available at my work machine (to which I am not having access right now)
  • It will ruin the joy of coding such a task. Do it yourself. You will enjot it.

Upvotes: 4

Lithu T.V
Lithu T.V

Reputation: 20021

Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}

Upvotes: -3

Firdous
Firdous

Reputation: 4652

that simple if you dont want the minus sign to appear, consider implementing you own solution than default one, insert a delete button and provide an animation to it, do as you require

Upvotes: 1

user2309710
user2309710

Reputation:

My idea, don't use button but u can swipe the cell to delete with the method:

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

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
         //Use actionsheet or deleteRowsAtIndexPaths
    }

 }

Upvotes: -2

Mani
Mani

Reputation: 571

You dont need a button action to trigger this. If you implement the following delegate you can directly swipe and delete the cell.

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    {
   //Delete the row
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

Upvotes: -2

Related Questions