当用户在聊天视图控制器中滑动表格视图单元格时,我想提供阻止和删除该用户的选项,或者只删除用户的聊天。有没有办法可以让滑动删除选项让两个选项都可用?我应该在另一个页面上添加块用户选项,还是可以在commit editStyle函数中同时使用这两个选项。
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))
}
})
}
}
答案 0 :(得分:2)
尝试此代码并替换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]
}
答案 1 :(得分:2)
下载/参考https://github.com/CEWendel/SWTableViewCell/archive/master.zip,将第三方库集成到您的项目中,并在ViewController中尝试以下代码 第1步: 将委托SWTableViewCellDelegate添加到ViewController 第2步: 在你的cellForRow
cell.leftUtilityButtons = leftButtons() as [AnyObject]
cell.rightUtilityButtons = self.rightButtons() as [AnyObject]
cell.delegate = self;
第3步: 在滑动上自定义左/右侧按钮
func leftButtons() -> NSMutableArray
{
let leftUtilityButtons : NSMutableArray = NSMutableArray()
leftUtilityButtons.sw_addUtilityButton(with: UIColor.orange, title: "Block")
leftUtilityButtons.sw_addUtilityButton(with: UIColor.green, title: "Remove User")
return leftUtilityButtons
}
func rightButtons() -> NSMutableArray {
let leftUtilityButtons : NSMutableArray = NSMutableArray()
leftUtilityButtons.sw_addUtilityButton(with: UIColor.red, title: "Delete Chat")
return leftUtilityButtons
}
第4步: 使用这两个委托方法处理动作
// click event on left utility button
func swipeableTableViewCell(_ cell: SWTableViewCell, didTriggerLeftUtilityButtonWith index: Int)
{
switch index
{
case 0:
// Handle your button1 action (Block User)
break
case 1: break
// Handle your button2 action (Remove User)
default:
break
}
}
// click event on right utility button
func swipeableTableViewCell(_ cell: SWTableViewCell, didTriggerRightUtilityButtonWith index: Int)
{
//handle your right button action (Delete Chat)
}
多数民众赞成......!
答案 2 :(得分:0)
您可以像这样修改您的功能
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let blockAction = UITableViewRowAction(style: .normal, title: "Block") { (rowAction, indexPath) in
self.muteConversation(self.conversations[indexPath.row])
}
let deleteAction = UITableViewRowAction(style: .destructive, title: "Only Delete") { (rowAction, indexPath) in
print("only delete selected ")
}
blockAction.backgroundColor = UIColor.gray
return [blockAction, deleteAction]
}
如果您想显示操作表,则可以使用此代码
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let moreAction = UITableViewRowAction(style: .normal, title: "More") { (rowAction, indexPath) in
self.showActionSheet(indexPath)
}
moreAction.backgroundColor = UIColor.blue
return [moreAction]
}
func showActionSheet(_ indexPath: IndexPath) {
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)
}