Saturday, June 8, 2013

UITableViewCell didTransitionToState example in Objective C (iOS).

UITableViewCell didTransitionToState

Called on the cell just after it transitions between cell states.

- (void)didTransitionToState:(UITableViewCellStateMask)state

Parameters of [UITableViewCell didTransitionToState]
A bit mask indicating the state or combination of states the cell is transitioning to.

Discussion of [UITableViewCell didTransitionToState]
Subclasses of UITableViewCell can implement this method to animate additional changes to a cell when it is changing state. UITableViewCell calls this method whenever a cell transitions between states, such as from a normal state (the default) to editing mode. This method is called at the end of the animation block, which gives the custom cell a chance to clean up after the state change—for example, removing the edit and reorder controls after transitioning out of editing. Subclasses must always call super when overriding this method.[UITableViewCell didTransitionToState]

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.

UITableViewCell didTransitionToState example.
- (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];

Example of [UITableViewCell didTransitionToState].
- (void)didTransitionToState:(UITableViewCellStateMask)state
//modified from comments here:

 if(state == UITableViewCellStateEditingMask)
  for (UIControl *control in self.subviews)
   // Find the Reorder Control
   if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellReorderControl")] && [control.subviews count] > 0)
    // Create Pointer to Reorder Controls Image View
    UIImageView *imageView = [control.subviews objectAtIndex:0];

    // Set Image Views Hidden Property to NO
    imageView.hidden = YES;

UITableViewCell didTransitionToState example.
You need to prepare the cell for reuse. Try adding this to the EditableCellStyle2 implementation:

- (void)prepareForReuse {
    [super prepareForReuse];
    [self didTransitionToState:UITableViewCellStateDefaultMask];

End of UITableViewCell didTransitionToState example article.