iOS自定义表格视图单元格在编辑模式下调整大小

时间:2013-01-06 21:40:33

标签: iphone ios xcode tableview

编辑UITableView圆形红色按钮时,删除按钮与自定义单元格重叠。

我们如何调整自定义单元格的大小,为红色圆形按钮和删除按钮留出空间。

2 个答案:

答案 0 :(得分:4)

使用此代码,您可以执行不同的任务,具体取决于编辑单元格的方式和阶段。我已经对代码进行了大量评论,因为我花了这么长时间来自行解决这个问题。 (变得混乱)

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

你说你添加了自定义标签,但没有,我在使用tableviews之前也做过同样的事情。我通常喜欢"隐藏"使用动画块重叠的视图,如:

[UIView animateWithDuration:0.3

                        animations:^ {
                            self.myTableCellSubview.alpha = 0.0f;
                        }
        ];

在上面的每个if语句中,并根据状态将alpha从1.0f更改为0.0f。

对于一般的缩进,在属性检查器中,确保"在编辑时缩进"已选中,您也可以使用以下命令设置:

cell.shouldIndentWhileEditing = YES;

如果这不起作用,您的自动调整可能会有一些奇怪之处。在故事板或xib中,选择需要缩进的单元格的子视图,然后转到“大小”检查器(标尺选项卡)。如果子视图需要从左侧缩进(--->),请确保它固定在左侧:

enter image description here

如果子视图需要从右侧缩进(< ---),请确保它固定在右侧:

enter image description here

希望这有帮助!

答案 1 :(得分:1)

您可以覆盖我认为的其中一种方法并调整自定义单元格的大小。根据这个问题,不确定这正是你所要求的。

- (void)willTransitionToState:(UITableViewCellStateMask)state
- (void)didTransitionToState:(UITableViewCellStateMask)state
相关问题