开发者

How can I change the amount of indentation on my custom UITableViewCell while editing?

开发者 https://www.devze.com 2023-02-26 10:58 出处:网络
I have made a custom UITableViewCell, and done it properly (adding everything to contentView, and overriding layoutSubviews so my subviews are relative to contentView.bounds).

I have made a custom UITableViewCell, and done it properly (adding everything to contentView, and overriding layoutSubviews so my subviews are relative to contentView.bounds).

When the user presses the Edit button, the table indents to allow room for the red delete sign. This is fine, but the default amount of indentation is too much, and ruins the look of my custom cell. How can I reduce the amoun开发者_开发百科t of indentation? setIndentationLevel and tableView:IndentationLevelForRowAtIndexPath don't seem to do anything.

(Someone asked a similar question here, but it was never resolved).


You've got to override layoutSubviews and do something like the following. And don't forget to set the indentation level to something greater than 0 :) For custom cells the indentation level is not applied by default.

To avoid indentation for the single swipe to delete gesture you'll have to do a but more work. There is a state which reflects the editing state of the cell.It is not public but can be accessed with - (void)willTransitionToState:(UITableViewCellStateMask)aState, so storing it in a property does the work for layoutViews.

Apple's documentation for willTransitionToState:

Note that when the user swipes a cell to delete it, the cell transitions to the state identified by the UITableViewCellStateShowingDeleteConfirmationMask constant but the UITableViewCellStateShowingEditControlMask is not set.

header file

int state;

...

@property (nonatomic) int state;

...

cell implementation

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


Nick Weaver's updated answer works great, except for the issue that Rec Levy pointed out:

Only thing now is that when you swipe-to-delete and then cancel (click elsewhere on the screen) the cell suddenly skips to the left and then slides back as the delete button disappears

I ran into the same problem. I'm not sure why it's occurring, but disabling animations while setting the contentView frame resolves it.

...
[UIView setAnimationsEnabled:NO];
self.contentView.frame = CGRectMake(indentPoints,
                                        self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width - indentPoints, 
                                        self.contentView.frame.size.height);
[UIView setAnimationsEnabled:YES];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号