删除期间UITableView更改

时间:2013-05-21 00:18:17

标签: uitableview

我有一个UITableView单元格,里面有一个自定义标签来处理变量高度。右侧和左侧还有一个UIImage。

当表格切换到编辑模式时,我想通知并更改表格中的每个单元格,以便正确格式化以便向右移动。并且,当用户按下小 - 时,我想进一步优化以便为删除按钮腾出空间。

寻找适合上述情况的模式,假设我控制的单元格中有自定义内容。

提前致谢!

1 个答案:

答案 0 :(得分:1)

Normaly你需要做的就是正确设置弹簧和支柱,你的内容将正确滑动。如果您在代码中创建子视图,则需要确保在addSubview而不是cell.contentView上调用cell

要隐藏和/或调整子视图的大小,您需要覆盖willTransitionToState:

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    UIView *imageView = self.rightImageView;
    UIView *labelView = self.centerTextLabel;

    CGRect labelFrame = labelView.frame;

    if (state & UITableViewCellStateShowingDeleteConfirmationMask) {

        labelFrame.size.width += 52;

        // Animating the fade while the image is sliding to the left
        // is more jarring then just making it go away immediately
        imageView.alpha = 0.0;

        [UIView animateWithDuration:0.3 animations:^{
            labelView.frame = labelFrame;
        }];

    } else if (!self.rightImageView.alpha) {

        labelFrame.size.width -= 52;

        [UIView animateWithDuration:0.3 animations:^{
            imageView.alpha = 1.0;
            labelView.frame = labelFrame;
        }];
    }

    [super willTransitionToState:state];
}

我在GitHub上创建了一个快速示例应用程序,它使用nib或代码演示iOS 4.3应用程序,只需取消注释//#define USE_NIB_TABLEVIEWCELL即可使用nib

https://github.com/GayleDDS/TestTableCell.git

enter image description here

我个人更喜欢在nib文件中创建表格视图单元格,并且只有在对应用程序进行概要分析后,才能用代码替换nib。