0

スワイプメニューを閉じたい

スワイプでメニューを出し、Doneアクションでチェックマークをつけて、メニューを閉じたいのですが、どうすればいいですか?

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: UIContextualAction.Style.normal, title: "Done") { (action, view, completionHandler) in
        let cell = tableView.cellForRow(at:indexPath)
        cell?.accessoryType = UITableViewCell.AccessoryType.checkmark
    }
    return UISwipeActionsConfiguration(actions: [action])
}

1 件の回答 1

0

UIContextualActionのインスタンス作成時に渡すhandler:クロージャーの実装方法が誤っています。処理が全て完了した後には、必ずcompletionHandlerを呼んでやらないといけません。

    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: UIContextualAction.Style.normal, title: "Done") { (_, _, completionHandler) in
            let cell = tableView.cellForRow(at:indexPath)
            cell?.accessoryType = .checkmark
            completionHandler(true) //<-actionが実行された場合、`true`を渡す
        }
        return UISwipeActionsConfiguration(actions: [action])
    }
1
  • ありがとうございます。completionHandlerは意味のあるものだったのですね。
    – zunda
    2021年4月19日 4:32

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。