2012-09-14 119 views
10

我知道UITableViewCell的子类可以实现willTransitionToState并在转换时执行自定义代码。但是有什么方法可以找到一个单元的当前状态?确定单元格的当前状态

如果不是,我应该继承UITableViewCell并定义属性currentState,我总是在我的willTransitionToState中更新?然后,我将总能知道任何特定单元格的状态。

看起来很奇怪,我不能问一个单元它的当前状态是什么(0,1,2或3)。

回答

15

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

这些状态对应于属性editingshowingDeleteConfirmation的值。它可以如下测试:

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 
} 
+1

许多谢谢。我的最终目标是识别显示删除确认的单元格,因为单元格已被滑动(而不是因为使用了编辑控件)。看看这些选项,看起来它还不够我测试,并且在两种情况下状态都是(3)(cell和tableview在两种情况下都是“编辑”)。回到绘图板。 –

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... 
    } 
} 
+0

在iOS 8.1 SDK(状态== UITableViewCellStateShowingDeleteConfirmationMask)应改为(状态&UITableViewCellStateShowingDeleteConfirmationMask),因为任何原因的高位位置都不为零。 – Neil

0

jhilgert00的SWIFT版本与尼尔的评论适用于:

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") 
    } 
} 
0

从SWIFT 3起始状态的值是一个OptionSet,你可以使用它像这样:

override func willTransitionToState(state: UITableViewCellStateMask) { 
    super.willTransitionToState(state) 
    if state.contains(.DefaultMask) { 
     print("DefaultMask") 
    } 
    if state.contains(.ShowingEditControlMask) { 
     print("ShowingEditControlMask") 
    } 
} 
相关问题