UITableView“滑动删除”按钮框架问题

时间:2013-01-14 20:02:20

标签: ios uitableview frame layoutsubviews

我在我的应用中使用自定义UITableViewCell,我正在尝试调整“滑动删除”按钮的框架。

这就是我正在做的事情:

- (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点,然后被移除。

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

1 个答案:

答案 0 :(得分:4)

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

自iOS 3.0起,两者都已推出。

将此代码放在UITableViewCell的子类中。它将处理从一个UITableViewCellStateMask到另一个{{3}}的所有转换,您可以实现转换到每个状态所需的动画。根据我添加的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

相关问题