13

I want to know what I am missing as my cell does not animate when hiding the delete button. The label jumps back to the original position before the delete button finish animating.

When I tap the round editing view to show the delete button, the label animation works:

Screenshot of cells where the delete button appears

However when I tap it again to hide the delete button, the movement of the label is not animated:

Screenshot of cells where the delete button disappears

Note: These screenshot are not created from the following code. But they show the problem.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    homeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Set up the cell
    Consumed *drinkObj = [self.appDelegate.consumedDrinksArray objectAtIndex:indexPath.row];        
    cell.titleLabel.text = drinkObj.drinkName;
    NSString *detailTextTime = [NSDate stringFromDate:drinkObj.dateConsumed withFormat:@"h:mma"];

    NSString *detailTextrelative = [relativeDateTransformer transformedValue:drinkObj.dateConsumed];

    NSString *detailText =  [NSString stringWithFormat:@"%@ %@ ", detailTextTime,detailTextrelative];
    cell.timeLabel.text = detailText;

    cell.stdDLabel.text = @"999.0"; //[NSString stringWithFormat:@"%@", drinkObj.standardDrinks];
    cell.stdDLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    cell.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    cell.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Desmond
  • 5,001
  • 14
  • 56
  • 115
  • 1
    I have the same problem, your video is a 404 and there was superfluous code. I think I improved your question. If you feel different feel free to roll it back. – Matthias Bauch May 03 '13 at 10:10
  • 1
    How does your `homeCell` class looks like? Which methods are you overriding? – Sulthan May 03 '13 at 10:38
  • 1
    Especially, is `layoutSubviews` overriden? – Sulthan May 03 '13 at 11:00
  • 1
    Could you please explain me what you want actually ?? do you want the label to auto adjusted ?? what problem you are facing ?? – Manu May 06 '13 at 13:31
  • 1
    The label should animate back into its original position. As seen in the screenshot it animates perfectly if the delete button is shown. But if the delete button will hide the label does not animate at all, it just jumps back into its original position. – Matthias Bauch May 07 '13 at 05:50
  • 1
    @MatthiasBauch see my answer. 99% it's a bug (I had it in one of my projects and tried to reproduce succesfully) and can be solved simply :-) – LombaX May 08 '13 at 10:52
  • 1
    I think when delete button shows your label moves, but when your delete button disappears, it looks like label is in initial postion already. So setting the label frame depending on delete button appearance will help you – Pavan May 09 '13 at 11:07
  • 1
    @PavanSaberjack I corrected my answer, I misunderstood the problem in origin. I think you are right, I attached some example code – LombaX May 09 '13 at 17:19

4 Answers4

7

EDIT: I see that the bug is different from what I understood in first instance. Let's correct my answer. If I'm right, your delete button disappears correctly with an animation, but the content view resizes itself without animating (going behind the button animating).

It this case you have to manage the animation yourself. I give you the code assuming you are not supporting rotation. If you want to support rotation, you'll need more code :-)

Here is a sample project (there is a lot of code because I used the standard master-detail xcode template, look only at the custom cell): Sample Project

To apply it to your code:

First thing, change the autoresizing mask of the labels on the right side of the cell to anchor on the LEFT side.

I'm not sure if the right label is stdDLabel, I'll assume it

// if the right margin is flexible, the left-top-bottom are fixed
cell.stdDLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

In your cell subclass override the setEditing:animated: method like this:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // this is an array of the labels / view that you want to animate
    NSArray *objectsToMove = @[self.stdDLabel];
    // this is the amount of pixel to move - the width of the delete button
    float pixelToMove = 70.0f;

    float animationDuration = 0.3f;

    // calculating the delta. If set editing, move from right to left. otherwise, from left to right
    float delta = (editing) ? -pixelToMove : pixelToMove;

    // put the move code in a block for avoid repeating code in the if
    void (^moveBlock)() = ^{
        for (UIView *view in objectsToMove) {
            view.center = CGPointMake(view.center.x + delta, view.center.y);
        }
    };

    // we want to move the labels only if editing is different from the current isEditing state
    if (editing != self.isEditing)
    {
        // execute the move block, animated or not
        if (animated)
            [UIView animateWithDuration:animationDuration animations:moveBlock];
        else
            moveBlock();
    }

    // call the super implementation
    [super setEditing:editing animated:animated];
}
LombaX
  • 17,265
  • 5
  • 52
  • 77
  • 1
    Since I'm always in editing mode `setEditing:animated:` won't help me much, but it's a good answer anyway. I think I'll just treat this behaviour as a bug in apples frameworks. Won't fix, to much work for little benefit. Thank you. – Matthias Bauch May 10 '13 at 08:20
  • 1
    You are right, I didn't consider that you are always in editing mode. I think (will try later) that you can solve overriding `willTransitionToState:` method, that is called every time the cell state is changed with a parameter of type UITableViewCellStateMask – LombaX May 10 '13 at 09:24
3

I didn't check all of your code, but for sure you'll need to add begin/end updates on each side of the delete...

[self.drinksTableView beginUpdates];
[self.drinksTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.drinksTableView endUpdates];

...to get the animation.

danh
  • 62,181
  • 10
  • 95
  • 136
  • 1
    Hi danh, thanks for the reply.the delete row animation works. i mean when at first selecting the "-" to "|", the animation works. however when changing from "|" to "-", there isn't any animation. – Desmond Sep 07 '12 at 05:02
3

You can perform this task using gesturerecognizer. It may help you to manage your cell.and also add a custome delete button on cell instead of default delete button.

Divyam shukla
  • 2,038
  • 14
  • 18
2

in the tableView:didEndEditingRowAtIndexPath: peform the task of re-locating the UILabels manually by settign their frames

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    homeCell *cell = (homeCell *)[tableView cellForRowAtIndexpath:indexPath];

    UILabel *labelToBeRelocated = (UILabel *)[cell viewWithTag:YOUR_LABEL_TAG];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25f];

    [labelToBeRelocated setFrame:CGRectMake(New_PosX , New_PosY , width , height)];

    [UIView commitAnimations];
}

Since the above delegate method is called after the EditingButton (Delete Button) is hidden in the UITableViewCell , hence the UILabel will be re-locating its position only after the delete button will be hidden . Hope it will help you.

Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
  • 1
    This won't work. If the label is right-anchored, it will jump immediately because the contentView changes its frame without animation (bug?). If you left-anchor the label, then you have to move right-to-left when the button appears, and left-to-right when disappear (in another method). See my solution – LombaX May 10 '13 at 06:25