WillTransitionToState (UITableViewCell) is not called in a specific situation

What i want to achieve

I have a custom UITableViewCell with a UITextField for in-place editing. The cell's field should be enabled while the UITableView is in edit mode, but NOT while confirmation of deleting the cell is displayed.

How do i achieve this

I subclassed UITableViewCell and overrided its willTransitionToState method:

 - (void)willTransitionToState:(UITableViewCellStateMask)state { [super willTransitionToState:state]; self.nameField.enabled = !(state & UITableViewCellStateShowingDeleteConfirmationMask) && (state & UITableViewCellStateEditingMask); } 

Problem

I'm 90% from there.

' willTransitionToState ' is called after the user clicks the "-" button of the cell. A confirmation message is displayed, and my text box is disabled as desired. But what if the user decides not to delete the cell and hides the confirmation of deletion by checking the right? In this case, 'willTransitionToState' is not called.

As a result, my text box is stuck in a disabled state, although it should be turned on when confirmation of deletion is hidden. You might think that given the fact that the " UITableViewCellStateShowingDeleteConfirmationMask " flag exists, " willTransitionToState " will be called symmetrically, but that doesn't seem to be the case.

UPDATE

It seems the UITableViewCell ' showingDeleteConfirmation ' property always gives the correct result. Therefore, theoretically, I could showingDeleteConfirmation over each cell that calls " showingDeleteConfirmation ", and accordingly enable or disable each text field. It is inefficient and kludgy. I am considering reporting a " willTransitionToState " error, but I need more data. Has anyone else encountered this problem?

04/29/2014

Apple confirms that this is a mistake. To date, the error report is still open.

+7
ios cocoa-touch uitextfield uitableview
source share
1 answer

Declare context for KVO:

 static int KVOContext; 

Get scrollView in a cell with a custom recipient:

 - (UIScrollView *)scrollViewToObserve { UIView *view = self.subviews[0]; return (view != nil && [view isKindOfClass:[UIScrollView class]]) ? (UIScrollView *)view : nil; } 

Add observer to scrollView contentOffset :

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self.scrollViewToObserve addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:&KVOContext]; } return self; } 

Remove observer in dealloc :

 - (void)dealloc { [self.scrollViewToObserve removeObserver:self forKeyPath:@"contentOffset" context:&KVOContext]; } 

Use KVO to monitor changes to contentOffset , but use showingDeleteConfirmation for the enabled state:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == &KVOContext) { if ([keyPath isEqualToString:@"contentOffset"]) { self.textField.enabled = !self.showingDeleteConfirmation; } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } 

Warning : viewing hierarchy may change in future updates

0
source share

All Articles