【问题标题】:Table view to navigate to a view controller and other actions in other cells表格视图导航到视图控制器和其他单元格中的其他操作
【发布时间】:2018-03-19 02:26:40
【问题描述】:

我在一个表格 (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)
}

我的问题是关于第三个和第四个单元格,我如何发送操作?

第三个单元格:您必须使用“SFSafariViewController”打开一个链接

第四个:点击时必须拨打指定号码。

Here an image of my table

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

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    要在 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)
            }
        }
    }
    

    【讨论】:

    • 谢谢,它非常适合我。再次,感谢您的帮助。问候
    • 很高兴为您提供帮助
    【解决方案2】:

    我在 swift 4 中的最终代码

    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
    
            let urlGruasWeb = URL(string: "https://www.google.com/")
            let vistaGruas = SFSafariViewController(url: urlGruasWeb!)
    
            present(vistaGruas, animated: true, completion: nil)
            vistaGruas.delegate = self as? SFSafariViewControllerDelegate
    
    
        default: //For fourth cell
    
            let url: NSURL = URL(string: "tel://\(911)")! as NSURL
            UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2016-03-31
      • 1970-01-01
      相关资源
      最近更新 更多