Saturday, June 8, 2013

UITableViewCell UITableViewCellStateDefaultMask example in Objective C (iOS).


UITableViewCell UITableViewCellStateDefaultMask

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 UITableViewCellStateDefaultMask]
The methods that use these constants are didTransitionToState: and willTransitionToState:.

UITableViewCell UITableViewCellStateDefaultMask example.
- (void)willTransitionToState:(UITableViewCellStateMask)state {
    if (state == UITableViewCellStateDefaultMask) {
        // User has switched back to default state...
        // ...
    }
}

Example of [UITableViewCell UITableViewCellStateDefaultMask].
- (void)didTransitionToState:(UITableViewCellStateMask)state {

    [super didTransitionToState:state];

    if (state == UITableViewCellStateShowingDeleteConfirmationMask || state == UITableViewCellStateDefaultMask) {
        for (UIView *subview in self.subviews) {

            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {

                UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
                CGRect f = deleteButtonView.frame;
                f.origin.x -= 20;
                deleteButtonView.frame = f;

                subview.hidden = NO;

                [UIView beginAnimations:@"anim" context:nil];
                subview.alpha = 1.0;
                [UIView commitAnimations];
            }
        }
    }
}

UITableViewCell UITableViewCellStateDefaultMask 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 UITableViewCellStateDefaultMask example article.