0

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.

enter image description here

enter image description here

Ganesh G
  • 1,991
  • 1
  • 24
  • 37

3 Answers3

2

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.

Bhavin
  • 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
  • @G.Ganesh : Can you Post your complete Error in your Question ? – Bhavin May 10 '13 at 13:50
0

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.

user1548843
  • 670
  • 12
  • 20
  • 1
    This 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
0

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;

}
Ganesh G
  • 1,991
  • 1
  • 24
  • 37