执行上下文菜单操作时如何从 TableViewCell 获取数组的索引
How to get the index of the array from a TableViewCell when executing a contextmenue action
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])
}
}
iOS13,Swift5
你好。这是我第一次在 TableViewCell 中实现上下文菜单。我做上面的事情。
它看起来很棒。但是我无法从 tableview 内容中获取 myPreparedTasks 数组的 ID。
我怎样才能得到这个ID?请给我这个构造的答案。我在网上看到很多其他的构造,但我还不明白。
非常感谢
詹斯
对于 table 视图中的上下文菜单,您必须实现委托方法
optional func tableView(_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration?
而不是向每个单元格添加 UIContextMenuInteraction
。
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])
}
}
iOS13,Swift5
你好。这是我第一次在 TableViewCell 中实现上下文菜单。我做上面的事情。 它看起来很棒。但是我无法从 tableview 内容中获取 myPreparedTasks 数组的 ID。 我怎样才能得到这个ID?请给我这个构造的答案。我在网上看到很多其他的构造,但我还不明白。
非常感谢 詹斯
对于 table 视图中的上下文菜单,您必须实现委托方法
optional func tableView(_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint) -> UIContextMenuConfiguration?
而不是向每个单元格添加 UIContextMenuInteraction
。