Mario Burga
Mario Burga

Reputation: 1157

Table view to navigate to a view controller and other actions in other cells

I have four cells in a table (UITableView), the first and second cells take me to a "ViewController" and with the following code works perfectly for me.

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

My question is with respect to the third and fourth cell, how do I send an action?

The third cell: you must open a link using "SFSafariViewController"

The fourth: when you click you must call a specified number.

Here an image of my table

I will appreciate if you can guide me

Upvotes: 0

Views: 338

Answers (2)

Mario Burga
Mario Burga

Reputation: 1157

My final code in 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)


    }
}

Upvotes: 0

Malik
Malik

Reputation: 3802

To open link in Safari, use

if let url = URL(string: "YOUR URL") {
    UIApplication.shared.openURL(url)
}

To call a number, use

if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
    UIApplication.shared.openURL(url)
}

Note:

You should only use performSegue for case 0 & 1. Also, I think your case 3 would actually be case 2. You can update your code to be as below

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

Upvotes: 1

Related Questions