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 に答える