Göktuğ Aral
Göktuğ Aral

Reputation: 1409

UITableViewCell, show swipe-style delete button without swipe

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;

}

Upvotes: 10

Views: 4412

Answers (3)

Bhadresh Sonani
Bhadresh Sonani

Reputation: 90

-(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 {

}

Upvotes: 1

Akash KR
Akash KR

Reputation: 798

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;
    }

Upvotes: 0

Hussein
Hussein

Reputation: 407

  • 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

Upvotes: 3

Related Questions