Saturday, June 8, 2013

UITableViewCell UITableViewCellStateShowingEditControlMask example in Objective C (iOS).


UITableViewCell UITableViewCellStateShowingEditControlMask

Cell State Mask Constants
Constants used to determine the new state of a cell as it transitions between states.

enum {
UITableViewCellStateDefaultMask = 0,
UITableViewCellStateShowingEditControlMask = 1 << 0,
UITableViewCellStateShowingDeleteConfirmationMask = 1 << 1
};

Constants
UITableViewCellStateDefaultMask
The normal state of a table cell.
UITableViewCellStateShowingEditControlMask
The state of a table view cell when the table view is in editing mode.
UITableViewCellStateShowingDeleteConfirmationMask
The state of a table view cell that shows a button requesting confirmation of a delete gesture.

Discussion of [UITableViewCell UITableViewCellStateShowingEditControlMask]
The methods that use these constants are didTransitionToState: and willTransitionToState:.

UITableViewCell UITableViewCellStateShowingEditControlMask example.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    NSString *logStr = @"Invoked";
    if ((state & UITableViewCellStateShowingEditControlMask)
        != 0) {
        // you need to move the controls in left
        logStr = [NSString stringWithFormat:@"%@
                  %@",logStr,@"UITableViewCellStateShowingEditControlMask"];
    }
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask)
        != 0) {
        // you need to hide the controls for the delete button
        logStr = [NSString stringWithFormat:@"%@
                  %@",logStr,@"UITableViewCellStateShowingDeleteConfirmationMask"];
    }
    NSLog(@"%@",logStr);
    [super willTransitionToState:state];
}

Example of [UITableViewCell UITableViewCellStateShowingEditControlMask].
- (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);   
    }
}

UITableViewCell UITableViewCellStateShowingEditControlMask example.
- (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...
    }
}

End of UITableViewCell UITableViewCellStateShowingEditControlMask example article.