簡體   English   中英

快速點擊tableviewcell打開網址

[英]tableviewcell open url on click swift

當我以編程方式單擊 tableview 中的單元格時,我一直試圖打開一個 url,而不必制作 webview 控制器並弄亂序列。 關於如何實現這一目標的任何幫助。 下面是我試過的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM