Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

}
like image 813
Göktuğ Aral Avatar asked Oct 20 '15 16:10

Göktuğ Aral


2 Answers

  • 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

like image 164
Hussein Avatar answered Oct 05 '22 23:10

Hussein


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

}
like image 45
Bhadresh Sonani Avatar answered Oct 05 '22 22:10

Bhadresh Sonani