如何使用拖放 UITableview 单元格 swift 启用前导和尾随轻扫



我正在尝试启用Leading并使用长按表视图单元格Trailing滑动以drag并使用Swiftdrop选项。在这里,我使用以下代码,我可以拖放它,但不能长按也不能一次启用前导和尾随swipe。应用启动时,默认情况下需要启用三项功能。

表视图委托

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
}

左动作领先

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])
}

这是对我有用的东西,但我不确定它是否正确,并且将来会一直有效

final class ViewController: UITableViewController {
private let data: [String] = [
"1", "2", "3", "4", "5"
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
cell.detailTextLabel?.text = data[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completion ) in
completion(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
}
extension ViewController: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
return []
}
}

最新更新