我在一个表 (UITableView) 中有四个单元格,第一个和第二个单元格将我带到“ViewController”,下面的代码非常适合我。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let segueIdentifier: String
switch indexPath.row {
case 0: //for first cell
segueIdentifier = "ubicacion"
case 1: //for second cell
segueIdentifier = "companias"
case 3: // For third cell
// Open un link using SFSafariViewController
default: //For fourth cell
// call phone
}
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
我的问题是关于第三个和第四个单元格,我该如何发送一个 Action ?
第三个单元格:您必须使用“SFSafariViewController”打开链接
第四条:点击时必须调用指定号码。
如果你能指导我,我将不胜感激
最佳答案
要在 Safari 中打开链接,请使用
if let url = URL(string: "YOUR URL") {
UIApplication.shared.openURL(url)
}
要调用号码,请使用
if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.openURL(url)
}
注意:
您应该只对情况 0 和情况 1 使用 performSegue
。此外,我认为您的情况 3 实际上是情况 2。您可以将代码更新为如下
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0: //for first cell
performSegue(withIdentifier: "ubicacion", sender: self)
case 1: //for second cell
performSegue(withIdentifier: "companias", sender: self)
case 2: // For third cell
if let url = URL(string: "YOUR URL") {
UIApplication.shared.openURL(url)
}
default: //For fourth cell
if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
}
关于ios - TableView 导航到 View Controller 和其他单元格中的其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354963/