Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Delete button On Editing in UITableView Cell

Implement this method in custom cell

- (void)willTransitionToState:(UITableViewCellStateMask)state{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
                [deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
                [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
                [deleteBtn release];
            }
        }
    }
}

This is a late reply but I hope someone may find this helpful. So the accepted answer seems sort of complicated for me, and @user1684899's answer only works if you just want to change the look of the delete button. I want to completely change the appearance of the delete button (i.e. its frame, position, etc.), so my solution is to add my own delete button to my custom cell and keep it initially hidden, and only show it when the cell is in edit mode. What's more important, I want to hide iOS's original delete button and support backward compatibility, and here's my trick:

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        if (!IS_IOS_7){
            for (UIView *subview in self.subviews)
            {
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
                {
                    // hide original button
                    [[subview.subviews objectAtIndex:0] setHidden:YES];
                    // show my custom button
                    [self.deleteButton setHidden:NO];
                }
            }

        } else {
            for (UIView *subview in self.subviews) {
                for (UIView *subview2 in subview.subviews) {
                    if ([NSStringFromClass([subview2 class]) rangeOfString:@"Delete"].location != NSNotFound) {
                        // hide original button
                        [subview2 setHidden:YES];
                        // show my custom button
                        [self.deleteButton setHidden:NO];
                    }
                }
            }
        }
    } else {
        // hide my custom button otherwise
        [self.deleteButton setHidden:YES];
    }
}

And don't forget to add:

[cell.deleteButton addTarget:self action:@selector(deleteEntryAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];

in cellForRowAtIndexPath, so that you can add any thing you want when your delete button is clicked. Here is my result:

enter image description here


The systemwide standard for the intended list-dive-in action, as you said in the comment on luvieere's answer, would be to use the detail-disclosure (blue circled arrow) cell accessory, not the swipe gesture.

That said, if you still want to use the swipe action for this, there's no way to provide your own button without manually intercepting and completely reimplementing the swipe gesture, like what Tweetie does.