I am using a custom UITableViewCell in my app, and I am trying to adjust the frame of the “swipe to delete” button.

This is what I’m doing:

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

It shows up in the new position, which is great. However, when I click away from the button so that it disappears, the button seems to suddenly move about 10 points to the left, and then gets removed.

Why is this happening, and how can I fix it?

有帮助吗?

解决方案

I'm not familiar with the animation code you're using, but I would try using willTransitionToState (and, if needed, didTransitionToState) instead of layoutSubviews to handle animation during editing tableViewCells.

Both have been available since iOS 3.0.

Place this code in your subclass of UITableViewCell. It will handle all of the transitions from one UITableViewCellStateMask to another, and you can implement the animation you need for the transition to each state. Just implement the animations you require in the proper place according to the NSLogs I added. (again, not familiar with your animation code, but I did test it and saw results using this code)

- (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...
    }
}

And if you need something to happen after one of these events, just implement the same code, but in didTransitionToState. The same UITableViewCellStateMasks apply.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top