[英]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.