确定一个单元格的当前状态
我知道UITableViewCell
的子类可以在转换时实现willTransitionToState
并执行自定义代码。 但是有没有办法find一个单元格的当前状态?
如果没有,我应该willTransitionToState
UITableViewCell
并定义一个属性currentState
,我总是在我的willTransitionToState
更新? 那么我将永远有办法知道任何特定细胞的状态。
似乎很奇怪,我不能问一个单元格当前状态是什么(0,1,2或3)。
当前状态是UITableViewCellStateDefaultMask
(0), UITableViewCellStateShowingEditControlMask
(1), UITableViewCellStateShowingDeleteConfirmationMask
(2)和UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask
UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask
(3)。
这些状态对应于属性editing
和showingDeleteConfirmation
的值。 它可以被testing如下:
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 }
对于iOS 6,这是我的解决scheme:
适用于任何过渡状态,并处理滑动以删除手势。 将这段代码放在你的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... } }
尼尔评论的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") } }
从swift 3开始,state的值是一个OptionSet,你可以像这样使用它:
override func willTransitionToState(state: UITableViewCellStateMask) { super.willTransitionToState(state) if state.contains(.DefaultMask) { print("DefaultMask") } if state.contains(.ShowingEditControlMask) { print("ShowingEditControlMask") } }