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

时间:2013-05-08 13:38:16

标签: ios objective-c uitableview

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

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

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

时,将调用此方法
- (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's HIG

答案 1 :(得分:0)

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

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

}

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

答案 2 :(得分:0)

我用下面的代码解决了我的问题,但我不知道苹果是否会接受。我在自定义单元格中使用了小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;

}