26

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?

Community
  • 1
  • 1
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
  • Let me give an example. MPMovieplayer is private api. Then How YouTube could customize it? ( YouTube has added two more buttons add to favorites, email this video ). There should some way. Why apple just keep customization limited to themselves? – sagarkothari Oct 23 '09 at 22:11
  • To understand my previous comment - read this question. http://stackoverflow.com/questions/1596089/mpmovieplayercontroller-with-a-custom-button-on-its-toolbar – sagarkothari Oct 23 '09 at 22:11
  • There is a better an updated solution here http://stackoverflow.com/a/7395052/454165 – Paul N Jan 24 '13 at 15:24

4 Answers4

34

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];
            }
        }
    }
}
Max Strater
  • 540
  • 6
  • 17
  • @user1684899, Iam getting 2 errors when i copy this code in CustomCell Class. Error is Expected Expression. – Ganesh G May 09 '13 at 05:32
  • @G.Ganesh check out the square brackets on your code. Usually the error shows because you have a pair of square brackets that aren't needed. Just delete those and you'll be good to go. – tyegah123 May 10 '13 at 02:55
  • @G.Ganesh..I am sure you must have figured it out by now. Still, here is the reason. Copying from here and pasting to Xcode, results is some characters to be added in the beginning of each line. Not sure what those are but u'll have to delete them to get the code comipling. – Sahitya Tarumani Jul 02 '13 at 06:59
  • If it is for ios7 then you should use UITableViewCellDeleteConfirmationControl_Legacy – Saren Inden Sep 25 '13 at 14:18
7

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

huong
  • 4,534
  • 6
  • 33
  • 54
  • It's very unreasonable to downvote one's post without any comment. How is that supposed to be helpful at all? – huong Mar 05 '14 at 07:51
  • Just wondering what font you're using here? And was your app accepted by Apple using the method you've described? – sooper May 12 '14 at 23:29
  • @sooper That font is Museo Sans and I ended up using [another library](https://github.com/CEWendel/SWTableViewCell) instead of this so I can't be too sure if Apple'd approve this. – huong May 13 '14 at 02:08
  • 4
    I NSLog the subviews' class out. I didn't find UITableViewCellDeleteConfirmationControl. I only got couple of _UITableViewCellSeparatorView – Yi Jiang Oct 10 '14 at 04:51
1

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.

Marco
  • 14,977
  • 7
  • 36
  • 33
0

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.

  • Ok. But the client said that, when I flick on a cell, there should a button for going into list - playlist. orange colored. – sagarkothari Oct 23 '09 at 19:57
  • In that case, you'll have to draw the button yourself. –  Oct 23 '09 at 22:31
  • Hi Saurabh, I am using default functionality to show delete button. But my background is also red and so the delete button cannot be distinguished... so i've to change the color/image of that button.... how can it be done? – Satyam Jul 28 '12 at 07:24
  • @Satyamsvv you cannot do that, you'll have to create your own view's and move them around like Saurabh says. – Tieme Jan 02 '13 at 12:39
  • There is a better an updated solution here: http://stackoverflow.com/a/7395052/454165 – Paul N Jan 24 '13 at 15:23
  • 2
    This answer circumvents the actual question, the working answer provided by user1684899 should be accepted – gchbib Jun 19 '13 at 11:32
  • 15
    "Why change it?" - Because the boss told us to, that's why. – Chris Harrison Feb 11 '15 at 11:14