uitableviewcell 打开 url onclick swift

tableviewcell open url on click swift

我一直在尝试在我的 tableview 程序中单击一个单元格时打开 url,而不必制作 webview 控制器并弄乱 seques。关于如何完成这项工作的任何帮助。下面是我试过的代码

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



    if indexPath.row == 0 {
        NSURL(string: "App Store Link")!
    }
    else if indexPath.row == 1 {
        NSURL(string: "Send Us Feedback - Contact On Website")!
    }  else if indexPath.row == 2 {
        NSURL(string: "https://www.instagram.com/prs_app/")!
    }  else if indexPath.row == 3 {
        NSURL(string: "Snapchat")!
    }

}

感谢您的帮助。谢谢

您正在寻找

UIApplication.sharedApplication().openURL(NSURL(string: "http://www.example.com")!)

这将为您打开 Safari 浏览器

编辑对评论中问题的解释
最好添加枚举或其他常量,但这样就可以了:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let url : NSURL?

    switch indexPath.section{
    case 0:
        switch indexPath.row{
        case 0:
            url = NSURL(string: "http://section0.row0.com")
        case 1:
            url = NSURL(string: "http://section0.row1.com")
        default:
            return;
        }

    case 1:
        switch indexPath.row{
        case 0:
            url = NSURL(string: "http://section1.row0.com")
        case 1:
            url = NSURL(string: "http://section1.row1.com")
        default:
            return;
        }
    default:
        return;
    }

    if url != nil{
        UIApplication.sharedApplication().openURL(url!)
    }
}

我有其他解决方案,希望它有效

覆盖 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let link = sectionContent[indexPath.section][indexPath.row].link

    switch indexPath.section {
    // Leave us feedback section
//You can add as many sections you want.

    case 0:
        if let url = URL(string: link) {
            let safariController = SFSafariViewController(url: url)
            present(safariController, animated: true, completion: nil)
        }

    // Follow us section
    case 1:
        if let url = URL(string: link) {
            let safariController = SFSafariViewController(url: url)
            present(safariController, animated: true, completion: nil)
        }

     default:
         break
     }

     tableView.deselectRow(at: indexPath, animated: false)
 }