Dvole
Dvole

Reputation: 5795

How to remove delete button when editing UITableViewCell?

I want to customize my cell behavior - I want cell view to change and different buttons appear. I got half of it working - when I swipe I get the view to move.

But I have this delete button to the right side of cell, I want to get rid of it and use custom button instead of it. What should I use to remove it and where do I bind my button to do the same thing?

I tried using two methods, setEditing: and willTransitionToState, but that didn't help.

If I skip calling parent method in any of these, I don't get delete button as I want, but my view never returns to its original position after editing and other cells stop reacting to swipes. What do I need to do to fix this?

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:YES];

    if (editing) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformIdentity;
        }];
    }
}

-(void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];

    if (state == UITableViewCellStateShowingDeleteConfirmationMask) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
        }];
    }

    if (state == UITableViewCellStateDefaultMask) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformIdentity;
        }];
    }
}

Upvotes: 2

Views: 2030

Answers (2)

aggmang
aggmang

Reputation: 121

Since your cell is a subclass of UITableViewCell, and you don't want to use the stock delete you probably want to disable it. The bare basic way would be to return no editing style for any of the cells.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;

}

Some of the comments mentioned what you can do next.

  1. Add an additional view that is below the main view of your cell.
  2. Add a button on that view on the right in the same place the delete button was.
  3. Use either a swipe gesture recognizer to do a basic swipe or a pan gesture recognizer
    for dragging (normal behavior).
  4. Update the top view frame based on the translations.

Upvotes: 2

santhu
santhu

Reputation: 4792

no you can't change the delete button that appears in default UITableViewCell.

Instead, create a customClass of UITableViewCell.

or,
Hack its subView and find any subView which is of class UIButton and title text "delete", then change it.

Upvotes: 0

Related Questions