10

I search couple of article but I didn't find what I'm looking for. Basically, I want to show delete button on each row but I don't want use UITableView.editing property. Because it looks like this;

enter image description here

There will be "Edit" button. When user click on it, delete button will looks like swipe-style.

Is there any chance to show delete buttons like this;

enter image description here

Maybe there is a some way to deal with it. Otherwise, I'm going to create custom view for this.

Thanks for your advice.

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

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

       //Do something... 
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;

}
Göktuğ Aral
  • 1,409
  • 1
  • 13
  • 28
  • do u want delete button like image2 in tableview? – Bhavin Ramani Oct 21 '15 at 06:54
  • @bhavinramani yes I want to delete buttons will looks like image2. There will be "Edit" button and when you click on it all rows should looks like image2. – Göktuğ Aral Oct 21 '15 at 07:38
  • Custom tableview cell is the only way to go for you, as the delete button on the end of the cell would not be visible for all the cell in tableview at once (as you elaborated in your screen shot) – viral Jun 29 '16 at 06:35

3 Answers3

3
  • Add a boolean property: @property BOOL didPressEdit;
  • Add UIButton to UITableViewCell
  • When pressing edit, didPressEdit becomes TRUE and UITableView reloads, such that cell.deleteButton.hidden = !didPressEdit; which makes all Delete buttons available
  • When pressing delete, remove object from datasource array, reload tableview

Hope this helps

Hussein
  • 407
  • 5
  • 16
1
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        [self.heartCartTabel setEditing:NO];
        [self editButtonClicked:indexPath.row];
    }];
    moreAction2.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

        [self tableView:self.heartCartTabel commitEditingStyle: UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
        //        [self.heartCartTabel deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    return @[deleteAction, moreAction2];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (IBAction)editButtonClicked:(int)indexNumber {

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Bhadresh Sonani
  • 90
  • 1
  • 11
0

You can use a UITableView delegate method to ask for those actions. Implement this method as follows:

  - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     // Respond to the action.
     }];
    modifyAction.backgroundColor = [UIColor blueColor];
    return @[modifyAction];
}

You can of course return multiple actions and customize the text and background color.

Implementing this method is also required to make the row editable:

   - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath   {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [self.objects removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
     }

You need to call the method editActionsForRowAtIndexPath on your edit button click.

  -(void)buttonTouched:(id)sender{

        UIButton *btn = (UIButton *)sender;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
        [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
    }

  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
Akash KR
  • 778
  • 3
  • 11
  • I am not sure that you understood the question. OP has specific need for making the delete button available without swipe and for all rows in tableview on tap of edit button. I am not seeing any of that achieved with the answer you provided. – viral Jun 29 '16 at 06:31