[英]How do I disable the default Delete swipe action and display my custom swipe action instead?
我正在我的应用程序中实施前导/尾随滑动操作。
领先的滑动操作是加入/离开表格中的事件。 尾部滑动操作是删除一个事件。 这两个滑动操作都应该是有条件的,主要基于用户是否登录。
如果向左或向右用户挥笔,和用户没有登录,我要显示一个警告(“需要登录......”)。
如果用户已登录,则根据用户是否已加入活动,引导操作将有条件地标题为“离开”或“加入”。 仅当用户也是事件的创建者时,才会创建尾随的“删除”操作。
当我测试应用程序并且用户登录时,一切正常。 (在我决定添加条件元素之前,这是有效的。)
当我测试应用程序并且用户未登录时,前导滑动效果很好:我向左滑动(在我的情况下),警报弹出。 TableViewCell 中不会出现滑动操作。
拖尾滑动也显示警报并正确反应,但由于某种原因,它也显示“删除”操作,即使我的代码使用标题“废话”。 解除警报后,红色的“删除”操作仍然可见且可点击。
我还完全删除了“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)
}
我已经解决了你的问题。 我希望这对你有用。 在 trailingSwipeActions 方法中,将动作样式更改为正常,您将获得“废话”标题。
从 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])
}
并且,在下面的方法中添加.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)
}
基于 ChillY 对这个问题的回答( 为什么前导滑动动作也作为尾随动作重复? ),我意识到问题是我返回的是nil
而不是UISwipeActionsConfiguration(actions: [])
。
现在我只需要弄清楚为什么在执行操作后滑动没有消失。 有任何想法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.