2013-01-14 144 views
2

我在我的应用程序中使用自定义UITableViewCell,我试图调整“滑动删除”按钮的框架。UITableView“滑动删除”按钮框问题

这是我在做什么:

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) return; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.0f]; 
    for (UIView *subview in self.subviews) { 
     if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { 
      CGRect newFrame = subview.frame; 
      newFrame.origin.x = newFrame.origin.x - 25; 
      subview.frame = newFrame; 
     } else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) { 
      CGRect newFrame = subview.frame; 
      newFrame.origin.x = newFrame.origin.x - 25; 
      subview.frame = newFrame; 
     } 
    } 
} 

它在新的位置,这是伟大的显示出来。但是,当我从按钮上单击以使其消失时,该按钮似乎突然向左移动约10个点,然后被移除。

为什么会发生这种情况,我该如何解决?

+0

此代码是否在您的自定义TableViewCell类中? – jhilgert00

+0

是的,它是我自定义的UITableViewCell类。 –

回答

4

我不熟悉您使用的动画代码,但我会尝试使用willTransitionToState(如果需要的话didTransitionToState)而不是layoutSubviews在编辑tableViewCells期间处理动画。

从iOS 3.0开始都可以使用它们。

将此代码嵌入到您的子类别UITableViewCell中。它将处理从一个UITableViewCellStateMask到另一个的所有转换,并且您可以实现过渡到每个状态所需的动画。根据我添加的NSLog,只需在适当的位置实现所需的动画。 (再次,不熟悉你的动画代码,但我没有测试它,看到使用该代码的结果)

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

    if (state == UITableViewCellStateDefaultMask) { 

     NSLog(@"Default"); 
     // When the cell returns to normal (not editing) 
     // Do something... 

    } else if ((state & UITableViewCellStateShowingEditControlMask) && (state & UITableViewCellStateShowingDeleteConfirmationMask)) { 

     NSLog(@"Edit Control + Delete Button"); 
     // When the cell goes from Showing-the-Edit-Control (-) to Showing-the-Edit-Control (-) AND the Delete Button [Delete] 
     // !!! It's important to have this BEFORE just showing the Edit Control because the edit control applies to both cases.!!! 
     // Do something... 

    } else if (state & UITableViewCellStateShowingEditControlMask) { 

     NSLog(@"Edit Control Only"); 
     // When the cell goes into edit mode and Shows-the-Edit-Control (-) 
     // Do something... 

    } else if (state == UITableViewCellStateShowingDeleteConfirmationMask) { 

     NSLog(@"Swipe to Delete [Delete] button only"); 
     // When the user swipes a row to delete without using the edit button. 
     // Do something... 
    } 
} 

如果你需要的东西,这些事件中的一个之后发生,只是实现了相同的代码,但在didTransitionToState。同样的UITableViewCellStateMask适用。

+0

感谢这个答案,并与从这里回答:http://stackoverflow.com/questions/2104403/iphone-uitableview-delete-button修复了我的问题! –

+0

最后,我可以使用UITableViewCell的方法willTransitionToState检测用户滑动事件: – tounaobun