简体   繁体   中英

How to add image to delete button when we swipe on tableview cell

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.

在此输入图像描述

在此输入图像描述

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.

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.

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM