[英]How to trigger an action sheet alert on UITableview cell press on iPadOS
I have some code to bring up an action sheet when a cell in a UITableView is pressed.当按下 UITableView 中的单元格时,我有一些代码可以调出操作表。 It works fine on the phone but on the iPad it fails.
它在手机上运行良好,但在 iPad 上却失败了。 I have see other people with this problem, needing to reconfigure their action sheet alert code somehow, but all I've seen is for triggering an action sheet on a button press.
我看到其他人有这个问题,需要以某种方式重新配置他们的操作表警报代码,但我所看到的只是在按下按钮时触发操作表。 I've seen this post, but I could not figure out how to implement this provided sample code:
我看过这篇文章,但我不知道如何实现这个提供的示例代码:
currentPopoverpresentioncontroller.sourceView = cell
Here is my function to show the action sheet:这是我的 function 显示操作表:
func showActionSheet() {
let alert = UIAlertController(title: "Card Actions", message: "choose action", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { action in
print("edit tapped")
self.renameDeckAlert()
}))
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { action in
self.showDeleteAlert()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
}))
present(alert, animated: true)
}
And here is where I call the function:这里是我称之为 function 的地方:
extension EditViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell #\(indexPath.row)")
showActionSheet()
self.editTableView.deselectRow(at: indexPath, animated: true)
}
}
Can somebody tell me how to bring up an action sheet on iPadOS when a cell in a tableview is pressed?有人可以告诉我如何在按下表格视图中的单元格时在 iPadOS 上调出操作表吗?
You can pass in the indexPath
of the selected cell to your presenting function.您可以将所选单元格的
indexPath
传递给您呈现的 function。 Then, get the cell with cellForRow(at:)
:然后,使用
cellForRow(at:)
获取单元格:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showActionSheet(indexPath: indexPath) /// pass in the indexPath
self.editTableView.deselectRow(at: indexPath, animated: true)
}
/// ↓ add an argument label
func showActionSheet(indexPath: IndexPath) {
let alert = UIAlertController(title: "Card Actions", message: "choose action", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { action in
print("edit tapped")
}))
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { action in
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
}))
/// for iPad
if let popoverController = alert.popoverPresentationController {
/// get the cell
let cell = tableView.cellForRow(at: indexPath)
popoverController.sourceView = cell
popoverController.sourceRect = cell?.bounds ?? CGRect(x: 0, y: 0, width: 50, height: 50)
}
present(alert, animated: true)
}
Result:结果:
iPhone![]() |
iPad ![]() |
---|---|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.