Ben Packard Ben Packard - 3 months ago 13
iOS Question

Determining the current state of a cell

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).

Answer

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
}