2016-05-14 59 views
0

我一直在嘗試打開一個URL,當我點擊一個單元格在我的tableview programatticaly,而無需使webview控制器和seques混亂。任何幫助我如何才能做到這一點。以下是我試過的代碼tableviewcell打開網址點擊迅速

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

} 

我很感激幫助。由於

回答

3

您正在尋找

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!) 
    } 
} 
+0

我試過了,它的工作原理,但在更改每個單元格的網址的情況下,它只有goe s到單元格的第一個鏈接(0 - apple.com)。有沒有辦法讓每個細胞進入不同的環節? –

+0

「如果indexPath.row == 0 { UIApplication.sharedApplication()的OpenURL(NSURL(字符串: 」http://www.apple.com「))。 } 否則,如果indexPath.row == 1 UIApplication.sharedApplication()。openURL(NSURL(string:「http://www.apple.com」)!) } else if indexPath.row == 2 {UIApplication.sharedApplication()。openURL(NSURL( string:「http://www.instagram.com/prs_app」)!) } else if indexPath.row == 3 UIApplication.sharedApplication()。openURL(NSURL(string:「http://www.snapchat .com「)!) }' –

+0

好吧,你確定你點擊正確的單元格嗎?或者你確定你不會混淆行和部分?因爲如果你的'UITableView'有4行,你的代碼應該可以工作(有更清晰的方法來寫它) –