iPhone UITableView - Delete Button

后端 未结 6 1640
-上瘾入骨i
-上瘾入骨i 2020-11-27 10:41

I am using the \'swipe to delete\' functionality of the UITableView.

The problem is I am using a customised UITableViewCell which is create

6条回答
  •  死守一世寂寞
    2020-11-27 11:31

    I use a solution that on first sight looks kind of hacky but does the trick and is not relying on undocumented apis:

    /**
     * Transition to state
     */
    -(void)willTransitionToState:(UITableViewCellStateMask)state {
    
        if(state & UITableViewCellStateShowingDeleteConfirmationMask)
            _deleting = YES;
        else if(!(state & UITableViewCellStateShowingDeleteConfirmationMask))
            _deleting = NO;
        [super willTransitionToState:state];
    
    }
    
    /**
     * Reset cell transformations
     */
    -(void)resetCellTransform {
    
        self.transform = CGAffineTransformIdentity;
        _background.transform = CGAffineTransformIdentity;
        _content.transform = CGAffineTransformIdentity;
    }
    
    /**
     * Move cell around if we are currently in delete mode
     */
    -(void)updateWithCurrentState {
    
        if(_deleting) {
    
            float x = -20;
            float y = 0;
            self.transform = CGAffineTransformMakeTranslation(x, y);
            _background.transform = CGAffineTransformMakeTranslation(-x, -y);
            _content.transform = CGAffineTransformMakeTranslation(-x, -y);
        }
    }
    
    -(void)setFrame:(CGRect)frame {
    
        [self resetCellTransform];
        [super setFrame:frame];
        [self updateWithCurrentState];
    }
    
    -(void)layoutSubviews {
    
        [self resetCellTransform];
        [super layoutSubviews];
        [self updateWithCurrentState];
    }
    

    Basically this shifts the cell position and readjusts the visible portions. WillTransitionToState just sets an instance variable indicating whether the cell is in delete mode. Overriding setFrame was necessary to support rotating the phone to landscape and vice-versa. Where _background is the background view of my cell and _content is a view added to the contentView of the cell, holding all labels etc.

提交回复
热议问题