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

前端 未结 2 379
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 13:48

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

2条回答
  •  执笔经年
    2020-12-14 14:29

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

提交回复
热议问题