简体   繁体   English

如何禁用默认的删除滑动操作并改为显示我的自定义滑动操作?

[英]How do I disable the default Delete swipe action and display my custom swipe action instead?

I'm implementing leading/trailing swipe actions in my app.我正在我的应用程序中实施前导/尾随滑动操作。

The leading swipe action is to join/leave an event in the table.领先的滑动操作是加入/离开表格中的事件。 The trailing swipe action is to delete an event.尾部滑动操作是删除一个事件。 Both of these swipe actions should be conditional, based primarily on if the user is logged in or not.这两个滑动操作都应该是有条件的,主要基于用户是否登录。

If the user swipes left or right , and the user is not logged in, I want to display an alert ("Login required...").如果向左或向右用户挥笔,和用户没有登录,我要显示一个警告(“需要登录......”)。

If the user is logged in, the leading action will conditionally be titled "Leave" or "Join" depending on if the user has already joined the event or not.如果用户登录,则根据用户是否已加入活动,引导操作将有条件地标题为“离开”或“加入”。 The trailing "Delete" action will be created only if the user is the also the creator of the event.仅当用户也是事件的创建者时,才会创建尾随的“删除”操作。

When I test the app and the user is logged in, everything works perfectly.当我测试应用程序并且用户登录时,一切正常。 (That was working before I decided to add the conditional element.) (在我决定添加条件元素之前,这是有效的。)

When I test the app, and the user is not logged in, the leading swipe works perfectly: I swipe left (in my case), the alert pops up.当我测试应用程序并且用户未登录时,前导滑动效果很好:我向左滑动(在我的情况下),警报弹出。 No swipe action appears in the TableViewCell. TableViewCell 中不会出现滑动操作。

The trailing swipe also shows the alert and reacts correctly, but for some reason it's also showing a "Delete" action, even though my code uses the title "Blah".拖尾滑动也显示警报并正确反应,但由于某种原因,它也显示“删除”操作,即使我的代码使用标题“废话”。 After dismissing the alert, the red "Delete" action is still visible and clickable.解除警报后,红色的“删除”操作仍然可见且可点击。

I've also completely removed the "trailingSwipe..." method but the "Delete" action still appears, so I need to figure out where the default is so I can turn it off and/or override it.我还完全删除了“trailingSwipe...”方法,但“删除”操作仍然出现,所以我需要弄清楚默认值在哪里,以便我可以将其关闭和/或覆盖它。

How do I prevent the default Delete action from appearing and display my action instead?如何防止出现默认的删除操作并改为显示我的操作?

Here's my code for the leading swipe:这是我的主要滑动代码:

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])
        }
    }
}

Here's my code for the trailing swipe:这是我的尾随滑动代码:

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])
    }
}

And here's the code for the alert:这是警报的代码:

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)

}

I have solved your issue.我已经解决了你的问题。 I hope that will work for you.我希望这对你有用。 In trailingSwipeActions method change action style to normal, you will get "Blah" title.在 trailingSwipeActions 方法中,将动作样式更改为正常,您将获得“废话”标题。

Remove return nil from your if statement.从 if 语句中删除return nil

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])
}

And, add .setEditing(false, animated: true) in below method并且,在下面的方法中添加.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)

}

Based on ChillY's answer to this question ( Why is the leading swipe action also duplicated as a trailing action? ), I realized the problem was that I was returning nil instead of UISwipeActionsConfiguration(actions: []) .基于 ChillY 对这个问题的回答( 为什么前导滑动动作也作为尾随动作重复? ),我意识到问题是我返回的是nil而不是UISwipeActionsConfiguration(actions: [])

Now I just have to figure out why the swipes are not disappearing after the action has been executed.现在我只需要弄清楚为什么在执行操作后滑动没有消失。 Any ideas?有任何想法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableView滑动操作可删除并使用自定义视图进行编辑 - UITableView swipe action delete and edit with custom view 我可以使用titleForDeleteConfirmationButtonForRowAtIndexPath进行滑动操作来删除其他操作吗? - Can I use swipe to delete as swipe to do some other action using titleForDeleteConfirmationButtonForRowAtIndexPath? 如何检测UITableCell上的向右滑动并显示自定义按钮而不是“删除”按钮 - How can I detect a right swipe on UITableCell and display a custom button instead of Delete button 是否无法在 UITableViewCell 上的删除操作(滑动按钮)中使用自定义视图? - Is it impossible to use custom view in delete action ( swipe button ) on UITableViewCell? 通过尝试删除,对UITableViewCell的自定义编辑操作在滑动时崩溃 - Custom edit action on UITableViewCell crashes on swipe by trying to delete iOS - 如何查看表格视图单元格上的“滑动删除”操作? - iOS - How To Give A Peek At The "Swipe To Delete" Action On A Table View Cell? 如何禁用 UIPageViewController 的滑动手势? - How do I Disable the swipe gesture of UIPageViewController? 快速禁用表格视图单元格中的滑动操作 - Disable the swipe action in table view cell in swift UITableViewRowAction操作是在滑动而不是在点击时触发的 - UITableViewRowAction action is triggered on swipe instead on tap 通过单击按钮替换滑动删除操作 - Replace action of swipe to delete by clicking on a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM