隐藏删除按钮时,自定义uitableviewcell内容不动画

Des*_*ond 13 iphone cocoa-touch uitableview ios

我想知道我错过了什么,因为我的单元格在隐藏删除按钮时没有动画.删除按钮完成动画之前,标签会跳回原始位置.

当我点击圆形编辑视图以显示删除按钮时,标签动画有效:

删除按钮出现的单元格的屏幕截图

但是,当我再次点击它以隐藏删除按钮时,标签的移动不会动画:

删除按钮消失的单元格的屏幕截图

注意:这些屏幕截图不是从以下代码创建的.但他们表明了这个问题.

// 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;
}
Run Code Online (Sandbox Code Playgroud)

Lom*_*baX 7

编辑:我看到这个错误与我在第一个实例中理解的不同.让我更正我的答案.如果我是对的,你的删除按钮会在动画中正确消失,但是内容视图会在没有动画的情况下自行调整大小(在按钮动画后面).

在这种情况下,您必须自己管理动画.假设您不支持轮换,我会给您代码.如果你想支持轮换,你需要更多的代码:-)

这是一个示例项目(有很多代码,因为我使用了标准的主 - 细节xcode模板,只查看自定义单元格):示例项目

要将其应用于您的代码:

首先,更改单元格右侧标签的自动调整遮罩以锚定在左侧.

我不确定正确的标签是否是stdDLabel,我会假设它

// if the right margin is flexible, the left-top-bottom are fixed
cell.stdDLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
Run Code Online (Sandbox Code Playgroud)

在您的单元格子类中重写此setEditing:animated:方法:

-(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];
}
Run Code Online (Sandbox Code Playgroud)