繁体   English   中英

我们在tableview单元格上滑动时如何添加图像以删除按钮

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

当我们在tableview单元格上滑动时,我已经实现了代码来获取删除按钮(请参阅第一张图片)。 我想自定义它意味着我想添加图像来代替删除按钮(见第二张图片)。 我用谷歌搜索它,但我没有得到任何方法或代码。 每个显示正常删除按钮的地方。 如何添加图像以删除按钮。 任何想法!请帮助我。

在此输入图像描述

在此输入图像描述

将该按钮更改为其他内容可能不是一个好主意,用户希望行为保持一致。但是,您可以在自定义单元格中实现此方法。 用户执行滑动操作时将调用此方法:

- (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];
           }
       }
       } 
}

注意:在实现自定义之前,我更愿意浏览Apple的HIG

您可以在自定义单元格上添加自定义按钮和自定义图像。启动隐藏此按钮,然后在单元格(右侧或左侧)上添加滑动手势。 (这里是自定义单元类)

 swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(handleGesture:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeLeft];

现在处理手势只是隐藏unhidde自定义删除按钮

-(void) handleGesture:(UIGestureRecognizer*)gestureRecognizer{

           if([self.deleteBtn isHidden])
           {
              [self.deleteBtn setHidden:NO];
           }else{
              [self.deleteBtn setHidden:YES];
          }

}

你也可以把两个滑动手势一个用于显示按钮,另一个用于隐藏按钮。 我希望这可以帮助你。

我用下面的代码解决了我的问题,但我不知道苹果是否会接受。 我在自定义单元格中使用了小UIView(100x40)并在主UIViewController中滑动单元格时跟踪它。 我在“CellForRowAtIndexPath”方法中编写了下面的代码,并且还实现了方法。

 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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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