当我在tableview programatticaly中单击一个单元格时,我一直在尝试打开一个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")!
}
}
我很感激帮助。感谢
答案 0 :(得分: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!)
}
}
答案 1 :(得分:0)
我对此有其他解决方案,希望它可以解决
重写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)
}