class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIContextMenuInteractionDelegate {
@IBOutlet weak var plannerTableView: UITableView!
...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myPreparedTasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let task = self.myPreparedTasks[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskTableViewCell
cell.taskId?.text = task.id
let interaction = UIContextMenuInteraction(delegate: self)
cell.addInteraction(interaction)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
return self.makeContextMenu()
})
}
func makeContextMenu() -> UIMenu {
let add = UIAction(title: "Neuer Task", image: UIImage(systemName: "plus.square")) { action in
// Show the Task ID
}
return UIMenu(title: "Task", children: [add])
}
}iOS 13,Swift 5
嗨。这是我第一次在TableViewCell中实现上下文菜单。我做了上面的事情。而且看起来很棒。但是我无法从tableview内容中获得myPreparedTasks数组的ID。我怎么才能拿到这个身份证?请给我这个构造的答案。我在网络上看到了许多其他的构造,但我还不理解它。
非常感谢Jens
发布于 2020-09-25 21:49:47
对于表视图中的上下文菜单,您必须实现委托方法
optional func tableView(_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration?而不是将UIContextMenuInteraction添加到每个单元。
https://stackoverflow.com/questions/64064685
复制相似问题