I know that a subclass of UITableViewCell
can implement willTransitionToState
and execute custom code at the time of transition. But is there any way to find the current state of a cell?
If not, should I subclass UITableViewCell
and define a property currentState
, which I always update in my willTransitionToState
? I will then always have a way to know the state of any particular cell.
Seems strange that I can't ask a cell what its current state is (0, 1, 2, or 3).
The current states are UITableViewCellStateDefaultMask
(0), UITableViewCellStateShowingEditControlMask
(1), UITableViewCellStateShowingDeleteConfirmationMask
(2), and UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask
(3).
These states correspond to the values of the properties editing
and showingDeleteConfirmation
. It can be tested as follows:
if (!cell.editing && !cell.showingDeleteConfirmation) {
// 0 - UITableViewCellStateDefaultMask
} else if (cell.editing && !cell.showingDeleteConfirmation) {
// 1 - UITableViewCellStateShowingEditControlMask
} else if (!cell.editing && cell.showingDeleteConfirmation) {
// 2 - UITableViewCellStateShowingDeleteConfirmationMask
} else if (cell.editing && cell.showingDeleteConfirmation) {
// 3 - UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask
}
For iOS 6, here's my solution:
Works for any of the transition states AND handles the swipe to delete gesture as well. Place this code in your subclass of UITableviewCell.
- (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...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With