如何在编辑时更改自定义UITableViewCell上的缩进量?

Ric*_*evy 6 iphone cocoa-touch uitableview

我已经制作了一个自定义的UITableViewCell,并且正确地完成了它(将所有内容添加到contentView,并覆盖layoutSubviews所以我的子视图是相对的contentView.bounds).

当用户按下"编辑"按钮时,表格会缩进以留出红色删除符号的空间.这很好,但默认的缩进量太多,并且破坏了我的自定义单元格的外观.如何减少压痕量?setIndentationLevel并且tableView:IndentationLevelForRowAtIndexPath似乎什么也没做.

(有人在这里问过类似的问题,但从未解决过).

Nic*_*ver 15

您必须覆盖layoutSubviews并执行以下操作.并且不要忘记将缩进级别设置为大于0的值.对于自定义单元格,默认情况下不应用缩进级别.

为了避免单次滑动缩进以删除手势,您将不得不做更多的工作.有一个状态反映了单元格的编辑状态.它不是公共的,但可以使用- (void)willTransitionToState:(UITableViewCellStateMask)aState,因此将其存储在属性中可以完成layoutViews的工作.

Apple的willTransitionToState文档:

请注意,当用户滑动单元格以将其删除时,单元格将转换为UITableViewCellStateShowingDeleteConfirmationMask常量标识的状态,但未设置UITableViewCellStateShowingEditControlMask.

头文件

int state;

...

@property (nonatomic) int state;

...
Run Code Online (Sandbox Code Playgroud)

细胞实施

@synthesize state;

...

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.contentView.frame = CGRectMake(0,                                          
                                        self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width, 
                                        self.contentView.frame.size.height);

    if (self.editing
        && ((state & UITableViewCellStateShowingEditControlMask)
        && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) || 
            ((state & UITableViewCellStateShowingEditControlMask)
         && (state & UITableViewCellStateShowingDeleteConfirmationMask))) 
    {
        float indentPoints = self.indentationLevel * self.indentationWidth;

        self.contentView.frame = CGRectMake(indentPoints,
                                            self.contentView.frame.origin.y,
                                            self.contentView.frame.size.width - indentPoints, 
                                            self.contentView.frame.size.height);    
    }
}

- (void)willTransitionToState:(UITableViewCellStateMask)aState
{
    [super willTransitionToState:aState];
    self.state = aState;
}
Run Code Online (Sandbox Code Playgroud)