确定单元的当前状态

Ben*_*ard 10 cocoa-touch uitableview ios

我知道子类UITableViewCell可以willTransitionToState在转换时实现和执行自定义代码.但有没有办法找到一个细胞的当前状态?

如果没有,我应该继承UITableViewCell并定义一个属性currentState,我总是在我的更新willTransitionToState?然后我将总是有办法知道任何特定细胞的状态.

似乎很奇怪,我不能问一个单元格当前状态是什么(0,1,2或3).

Jef*_*mas 15

当前状态是UITableViewCellStateDefaultMask(0),UITableViewCellStateShowingEditControlMask(1),UITableViewCellStateShowingDeleteConfirmationMask(2)和UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask(3).

这些状态对应于属性editing和值showingDeleteConfirmation.它可以测试如下:

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
}
Run Code Online (Sandbox Code Playgroud)


jhi*_*t00 8

对于iOS 6,这是我的解决方案:

适用于任何过渡状态并处理滑动以删除手势.将此代码放在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...
    }
}
Run Code Online (Sandbox Code Playgroud)