ios - TableView 导航到 View Controller 和其他单元格中的其他操作

标签 ios iphone swift

我在一个表 (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”打开链接

第四条:点击时必须调用指定号码。

Here an image of my table

如果你能指导我,我将不胜感激

最佳答案

要在 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/

相关文章:

iphone - 改变 UIView 一小部分的颜色?

swift - insertSubVIew 占用 "long"时间

ios - UIDocumentPickerExtensionViewController validTypes 始终为零

ios - 通过 Stripe 付款错误显示在 html Swift 中

html - 如何使用 metatag 在 ios 设备的移动网站上禁用缩放?

ios - 我可以通过编程方式将 iPhone 设置为边缘网络吗?

ios - UITableViewCell 内部的程序化自动布局更改仅在方向更改后更新

ios - 如何在 ios 中使用 oauth 2 api 从 google plus 获取用户生日?

ios - UIScrollView显示错误位置

ios - Xcode 不打开我的项目