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

标签 iphone ios cocoa-touch uitableview

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

当我点击圆形编辑 View 以显示删除按钮时,标签动画起作用:

Screenshot of cells where the delete button appears

然而,当我再次点击它以隐藏删除按钮时,标签的移动没有​​动画:

Screenshot of cells where the delete button disappears

注意:这些屏幕截图不是由以下代码创建的。但它们显示了问题。

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

最佳答案

编辑: 我发现该错误与我最初理解的不同。 让我们更正我的答案。 如果我是对的,您的删除按钮会随着动画正确消失,但内容 View 会自行调整大小而不设置动​​画(在按钮动画后面)。

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

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

将其应用于您的代码:

首先,将单元格右侧标签的自动调整掩码更改为锚定在左侧。

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

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

在您的单元格子类中重写 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];
}

关于iphone - 隐藏删除按钮时自定义 uitableviewcell 内容不动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311090/

相关文章:

iphone - TableViewCell 中的多列

iphone - 应用程序中的 EXC_BAD_ACCESS 错误,Alamofire 3.1.2

ios - 使用核心数据创建新的托管对象

iphone - 应用内购买 - 消耗品

ios - Adobe AIR for iOS - 设备空间不足

ios - 检查 CGPoint 是否在给定 View 内?

ios - 在弹出它转换到的 View Controller 之前,如何让单元格保持突出显示?

iphone - 在保持最大项目总数的同时向历史记录添加新项目有什么技巧或最佳实践吗?

iPhone Simulator 6.0 无法在沙盒模式下连接到 gamecenter

iphone - 调用 rootViewController 在内容 View 中切换 View (iOS)