11

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

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

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

4

4 回答 4

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
}
于 2012-09-15T02:06:24.307 回答
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...
    }
}
于 2013-01-06T22:18:28.727 回答
0

应用了 Neil 评论的 jhilgert00 快速版本:

override func willTransitionToState(state: UITableViewCellStateMask) {

    super.willTransitionToState(state)

    if (state == UITableViewCellStateMask.DefaultMask) {
        println("default")
    } else if (state & UITableViewCellStateMask.ShowingEditControlMask != nil)
        && (state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil) {
            println("Edit Control + Delete Button")
    } else if state & UITableViewCellStateMask.ShowingEditControlMask != nil {
        println("Edit Control Only")
    } else if state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil {
        println("Swipe to Delete [Delete] button only")
    }
}
于 2015-03-21T19:55:47.283 回答
0

从 swift 3 开始,state 的值是一个 OptionSet,你可以像这样使用它:

override func willTransitionToState(state: UITableViewCellStateMask) {
    super.willTransitionToState(state)
    if state.contains(.DefaultMask) {
        print("DefaultMask")
    }
    if state.contains(.ShowingEditControlMask) {
        print("ShowingEditControlMask")
    }
}
于 2016-08-14T21:03:35.403 回答