I have implemented code to get delete button when we swipe on tableview cell(see first image). I want to custom it means i want to add image in place of delete button(see second image). I googled it but i didn't get any methods or code. Every where showing normal delete button. How can i add image to delete button. Any ideas!.Please help me.
-
Take a look at this posting: http://stackoverflow.com/questions/13669920/customize-the-delete-button-in-uitableview – sangony May 08 '13 at 13:43
-
2You should not do this. iOS apps should look like iOS apps. You shouldn't try to copy your other design too much. – Marcus Adams May 08 '13 at 14:18
-
@MarcusAdams: Agreed. Check My Answer. – Bhavin May 08 '13 at 14:21
-
Use the normal delete button and save yourself the trouble. – nielsbot May 14 '13 at 06:06
3 Answers
Probably not a good idea to change that button to something else, users expect that behavior remains consistent.However You can Implement this method in your Custom Cell. This method will be called when user performs the Swipe Action :
- (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];
}
}
}
}
Note : I will prefer to go through Apple's HIG before implementing the customisation.

- 27,155
- 11
- 55
- 94
-
(Directed more to the OP than Vin): Also keep in mind that code like this is extremely fragile and could break after any iOS update. It's never a good idea to dig into the private subviews of a standard view provided by Apple's framework. – rmaddy May 08 '13 at 15:11
-
Already i tried this code and some one posted same code also. But Iam getting 2 errors when i copy this code in to CustomCell Class. Error is Expected Expression. – Ganesh G May 09 '13 at 05:34
-
You can add your custom button with your custom image on custom cell.start hide this button and than add swipe gesture on cell(right or left )what you want . (here self is custom cell class )
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeLeft];
now in handle gesture just hide unhidde your custom delete button
-(void) handleGesture:(UIGestureRecognizer*)gestureRecognizer{
if([self.deleteBtn isHidden])
{
[self.deleteBtn setHidden:NO];
}else{
[self.deleteBtn setHidden:YES];
}
}
you can also put two swipe gesture one for display button and other one for hide button. i hope this may help you.

- 670
- 12
- 20
-
1This is the proper general approach. Use your own custom gesture handling and do not enable the standard swipe-to-delete delegate methods. – rmaddy May 08 '13 at 15:16
I solved my problem with below code but i don't know whether apple will accept or not. I have taken small UIView(100x40) in custom cell and tracking it when i swiped cell in main UIViewController. I wrote below code in "CellForRowAtIndexPath" Method and also implemented methods.
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureRight:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell addGestureRecognizer:swipeGesture];
-(void)handleSwipeGestureLeft:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"swipe left");
self.cell.UIViewObj.hidden= Yes;
}
-(void)handleSwipeGestureRight:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"swipe reght");
CGPoint swipeLocation = [gestureRecognizer locationInView:tblView];
NSIndexPath *swipedIndexPath = [tblView indexPathForRowAtPoint:swipeLocation];
NSLog(@"swipedIndexPath %d",[swipedIndexPath row]);
self.cell = (CustomCell*)[tblView cellForRowAtIndexPath:swipedIndexPath];
self.cell.UIViewObj.hidden= No;
}

- 1,991
- 1
- 24
- 37