[英]Table view to navigate to a view controller and other actions in other cells
我在一个表(UITableView)中有四个单元格,第一个和第二个单元格将我带到“ ViewController”,并且下面的代码对我来说非常合适。
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)
}
我的问题是关于第三和第四单元格,我该如何发送动作?
第三个单元格:您必须使用“ SFSafariViewController”打开链接
第四:单击时必须拨打指定的号码。
如果您能指导我,我将不胜感激
要在Safari中打开链接,请使用
if let url = URL(string: "YOUR URL") {
UIApplication.shared.openURL(url)
}
要拨打电话,请使用
if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.openURL(url)
}
注意:
您只应对情况0和1使用performSegue
。此外,我认为情况3实际上就是情况2。您可以将代码更新如下
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)
}
}
}
我在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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.