0

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

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

2

将该按钮更改为其他按钮可能不是一个好主意,用户希望该行为保持一致。但是您可以在自定义单元格中实现此方法。当用户执行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];
           }
       }
       } 
}

注意:在实施定制之前,我更喜欢通过Apple 的 HIG 。

于 2013-05-08T13:45:12.247 回答
0

您可以在自定义单元格上添加带有自定义图像的自定义按钮。开始隐藏此按钮,然后在单元格(右或左)上添加您想要的滑动手势。(这里 self 是自定义单元类)

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

现在在处理手势只是隐藏取消隐藏您的自定义删除按钮

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

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

}

您还可以放置两种滑动手势,一种用于显示按钮,另一种用于隐藏按钮。我希望这可以帮助你。

于 2013-05-08T14:18:16.803 回答
0

我用下面的代码解决了我的问题,但我不知道苹果是否会接受。当我在主 UIViewController 中滑动单元格时,我在自定义单元格中使用了小 UIView(100x40) 并跟踪它。我在“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;

}
于 2013-05-10T13:00:48.757 回答