gpt4 book ai didi

ios - 滑动删除多个选项

转载 作者:可可西里 更新时间:2023-11-01 00:39:25 26 4
gpt4 key购买 nike

当用户在聊天 View Controller 中滑动表格 View 单元格时,我想提供选项来阻止和删除该用户,或者只删除用户的聊天。有没有一种方法可以让滑动删除选项使这两个选项都可用?我应该在不同的页面上添加阻止用户选项,还是有办法在 commit editingStyle 函数中同时拥有这两者。

class Conversation {
var key:String
var sender:String
var recipient:String
var date:Date
var recentMessage:String
var seen:Bool

init(key:String, sender: String, recipient:String, date:Date, recentMessage:String, seen:Bool) {
self.key = key
self.sender = sender
self.recipient = recipient
self.date = date
self.recentMessage = recentMessage
self.seen = seen
}

// Returns the UID of the conversations partner
// i.e NOT the UID of the current user
var partner_uid:String {
guard let uid = Auth.auth().currentUser?.uid else { return "" }
if sender != uid {
return sender
}
return recipient
}

func printAll() {
print("key: \(key)")
print("sender: \(sender)")

print("recentMessage: \(recentMessage)")
}
}

class ChatsTableViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView:UITableView!

var conversations = [Conversation]()

override func viewDidLoad() {
super.viewDidLoad()

tableView = UITableView(frame: view.bounds)
let nib = UINib(nibName: "ChatTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "chatCell")
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()

title = "CHAT"
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let cell = tableView.cellForRow(at: indexPath) as! ChatTableViewCell
let name = cell.usernameLabel.text!

let actionSheet = UIAlertController(title: "Block conversation with \(name)?", message: "Further messages from \(name) will be muted.", preferredStyle: .alert)

let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in }



actionSheet.addAction(cancelActionButton)

let deleteActionButton: UIAlertAction = UIAlertAction(title: "Block", style: .destructive)
{ action -> Void in
self.muteConversation(self.conversations[indexPath.row])
}

let deleteOnlyButton: UIAlertAction = UIAlertAction(title: "Only Delete", style: .destructive)
{ action -> Void in


print("only delete selected ")
}

actionSheet.addAction(deleteActionButton)
actionSheet.addAction(deleteOnlyButton)
self.present(actionSheet, animated: true, completion: nil)
}
}

func muteConversation(_ conversation:Conversation) {
guard let user = Auth.auth().currentUser else { return }
let ref = Database.database().reference()

let obj = [
"social/blocked/\(user.uid)/\(conversation.partner_uid)" : true,
"social/blockedBy/\(conversation.partner_uid)/\(user.uid)" : true,
"conversations/users/\(user.uid)/\(conversation.partner_uid)/muted": true
] as [String:Any]
print("OBBJ: \(obj)")
ref.updateChildValues(obj, withCompletionBlock: { error, ref in
if error != nil {
let alert = UIAlertController(title: "Error deleting conversation!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
} else {
let alert = UIAlertController(title: "Conversation blocked!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
}
})
}

最佳答案

尝试此代码并将 Action1 和 Action2 替换为您喜欢的操作。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let action1 = UITableViewRowAction(style: .default, title: "Action1", handler: {
(action, indexPath) in
print("Action1")
})
action1.backgroundColor = UIColor.lightGray
let action2 = UITableViewRowAction(style: .default, title: "Action2", handler: {
(action, indexPath) in
print("Action2")
})
return [action1, action2]
}

关于ios - 滑动删除多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48515945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com