gpt4 book ai didi

ios - 如何通过拖放 UITableview Cell Swift 启用前导和尾随滑动

转载 作者:行者123 更新时间:2023-11-28 23:21:56 25 4
gpt4 key购买 nike

我正在尝试启用 LeadingTrailing 使用长按 tableview 单元格滑动到 dragdrop 选项使用 Swift。在这里,我使用下面的代码我可以拖放它但不能长按也不能同时启用前导和尾随 swipe。应用启动时需要默认启用三项功能。

TableView 委托(delegate)

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Delete")
success(true)
})
deleteAction.backgroundColor = .orange
return UISwipeActionsConfiguration(actions: [deleteAction])
}

override func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let modifyAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
self.showaddMilestone()
success(true)
})
modifyAction.image = UIImage(named: "edit")
modifyAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [modifyAction])
}

override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedObject = self.milestoneTitles[sourceIndexPath.row]
milestoneTitles.remove(at: sourceIndexPath.row)
milestoneTitles.insert(movedObject, at: destinationIndexPath.row)
debugPrint("\(sourceIndexPath.row) => \(destinationIndexPath.row)")
// To check for correctness enable: self.tableView.reloadData()
}

最佳答案

你可以通过以下方式做到这一点......

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

//EDIT
let actionEDIT = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionEDIT.image = UIImage(named: "icn_edit")
actionEDIT.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

//PDF
let actionPDF = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionPDF.image = UIImage(named: "icn_pdf")
actionPDF.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

//SHARE
let actionSHARE = UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
actionSHARE.image = UIImage(named: "icn_shareGreen")
actionSHARE.backgroundColor = UIColor.UIColorFromHex(hex: "F7F7F7")

let configuration = UISwipeActionsConfiguration(actions: [actionSHARE,actionPDF,actionEDIT])

return configuration
}

//leftAction 前导

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let leftAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("leftAction tapped")
success(true)
})

leftAction.image = UIImage(named: "")
leftAction.backgroundColor = UIColor.red

return UISwipeActionsConfiguration(actions: [leftAction])
}

关于ios - 如何通过拖放 UITableview Cell Swift 启用前导和尾随滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526339/

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