首页 > 解决方案 > 如何禁用默认的删除滑动操作并改为显示我的自定义滑动操作?

问题描述

我正在我的应用程序中实现前导/尾随滑动操作。

主要的滑动动作是加入/离开表格中的事件。尾随滑动动作是删除事件。这两个滑动操作都应该是有条件的,主要基于用户是否登录。

如果用户向左或向右滑动,并且用户没有登录,我想显示一个警报(“需要登录...”)。

如果用户登录,则根据用户是否已加入活动,引导操作将有条件地标题为“离开”或“加入”。仅当用户也是事件的创建者时,才会创建尾随的“删除”操作。

当我测试应用程序并且用户登录时,一切正常。(在我决定添加条件元素之前,这是可行的。)

当我测试应用程序并且用户未登录时,领先的滑动效果很好:我向左滑动(在我的情况下),弹出警报。TableViewCell 中不会出现滑动操作。

尾随滑动也显示警报并做出正确反应,但由于某种原因,它也显示“删除”操作,即使我的代码使用标题“Blah”。解除警报后,红色的“删除”操作仍然可见且可单击。

我还完全删除了“trailingSwipe ...”方法,但“删除”操作仍然出现,所以我需要弄清楚默认值在哪里,以便我可以将其关闭和/或覆盖它。

如何防止出现默认删除操作并改为显示我的操作?

这是我的领先滑动代码:

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    if currentUserID == nil {
        showLoginRequiredMessage()
        return nil
    } else {
        var userName = people.randomElement() // for testing

        if event.eventMembers.contains(userName) {
            let index = event.eventMembers.firstIndex(of: userName)!
            let leaveAction = UIContextualAction(style: .normal, title: "Leave") { (action, view, nil) in
                event.eventMembers.remove(at: index)
                tableView.setEditing(false, animated: true)
                tableView.reloadRows(at: [indexPath], with: .automatic)
                self.saveEvents()
            }

            leaveAction.backgroundColor = .red

            return UISwipeActionsConfiguration(actions: [leaveAction])
        } else {
            let joinAction = UIContextualAction(style: .normal, title: "Join") { (action, view, nil) in
                event.eventMembers.append(userName)
                tableView.setEditing(false, animated: true)
                tableView.reloadRows(at: [indexPath], with: .automatic)
                self.saveEvents()
            }

            joinAction.backgroundColor = .green

            return UISwipeActionsConfiguration(actions: [joinAction])
        }
    }
}

这是我的尾随滑动代码:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    if currentUserID == nil {
        showLoginRequiredMessage()
        return nil
    } else {
        let trailingAction = UIContextualAction(style: .destructive, title: "Blah") { (action, view, nil) in
            tableView.setEditing(false, animated: true)
            print ("Delete this event")
        }
        trailingAction.backgroundColor = .red
        return UISwipeActionsConfiguration(actions: [trailingAction])
    }
}

这是警报的代码:

private func showLoginRequiredMessage() {
    let ac = UIAlertController(title: "Login Required", message: "To modify an event, you must first login", preferredStyle: .alert)

    ac.addAction(UIAlertAction(title: "Sign In", style: .default, handler: {(action) in
        if let authenticationController = self.storyboard?.instantiateViewController(withIdentifier: "authenticationScreen") {
            self.present(UINavigationController(rootViewController: authenticationController), animated: true)
        }
    }))

    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    present(ac, animated: true)

}

标签: iosswiftuiswipeactionsconfiguration

解决方案


我已经解决了你的问题。我希望这对你有用。在 trailingSwipeActions 方法中将动作样式更改为正常,您将获得“Blah”标题。

return nil从您的 if 语句中删除。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    if currentUserID == nil {
        self.showLoginRequiredMessage()
    }
    let trailingAction = UIContextualAction(style: .normal, title: "Blah") { (action, view, boolval) in
        print ("Custom action event")
        tableView.setEditing(false, animated: true)
    }
    trailingAction.backgroundColor = .gray
    return UISwipeActionsConfiguration(actions: [trailingAction])
}

并且,添加.setEditing(false, animated: true)以下方法

private func showLoginRequiredMessage() {
    let ac = UIAlertController(title: "Login Required", message: "To modify an event, you must first login", preferredStyle: .alert)

    ac.addAction(UIAlertAction(title: "Sign In", style: .default, handler: {(action) in
        self.myTableView.setEditing(false, animated: true)
        if let authenticationController = self.storyboard?.instantiateViewController(withIdentifier: "authenticationScreen") {
            self.present(UINavigationController(rootViewController: authenticationController), animated: true)
        }
    }))

    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(action) in
        self.myTableView.setEditing(false, animated: true)
    }))
    present(ac, animated: true)

}

推荐阅读