Question

I have gone through this question that shows the following code:

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"Sagar";
}

What if I want a custom image instead of the default red button?

Was it helpful?

Solution

luvieere is right -- if you want that same "delete" metaphor, you want to keep it at the red button that Apple provides. Why change it? Its globally recognizable as the standard for delete buttons.

Although, if you want something like Tweetie, where you completely change the behavior of swiping, you could use something like ABTableViewCell where you just draw in your own view. Make a custom table view, override -touchesBegan:withEvent:, and check for touches. Calculate the delta of the two touches (in touchesMoved:withEvent:) and move your own view around.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top