2016-11-19 4 views
0

私はUITableViewに5つのスタティックセルを持っています。UITableViewCellでURLを開く方法

セルの1つをタップすると、特定のURLが開きます。各セルには固有のURLがあります。

どうすればいいですか?

Swift 3、Xcodeを使用しています。

ありがとうございます!

コード:

import UIKit 
import Kingfisher 

class SettingsTableView: UITableViewController { 

    @IBAction func clearCache(_ sender: Any) { 


     ImageCache.default.clearMemoryCache() 

     ImageCache.default.clearDiskCache() 

    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 

     self.navigationController!.navigationBar.barTintColor = UIColor.black 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     self.navigationController?.hidesBarsOnTap = false 

    } 

    override var prefersStatusBarHidden: Bool { 
     return true 
    } 

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

} 
+0

Use- 'オーバーライドのfuncのtableView(_のtableView:のUITableView、didSelectRowAt indexPath:IndexPath){//あなたのコード ... }私は理解していない@iDeveloper' – iDeveloper

+0

。コードを返信してください。ありがとうございました。 – Miles

+0

URLを開くには、tableViewCellをクリックしますか? – iDeveloper

答えて

0

あなたがデキューされたカスタムセルクラスにvar urlToBeOpened : String性質を持っていると仮定します。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
       let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell 
       let urlAsString = cell.urlToBeOpened 
       let url = URL(string : urlAsString) 
       UIApplication.sharedApplication().openURL(url) 

} 

それはURLエンコードやURL(string : urlAsString)があなたのdidSelectRowAt indexPath関数内

0

nilを返しますurlToBeOpened文字列は、あなたがグローバル配列を宣言代わりopenURL

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let url = URL(string: self.urlArray[indexPath.row])! 
    UIApplication.shared.open(url, options: [:]) 
} 
0

openを呼び出す必要がありますことを確認します(あなたのクラスの中で)そのようなリンクで

let links = ["http://google.com", "https://apple.com"] 

はその後にdidSelectRowAtは単に

let link = links[indexPath.row] 
if let url = URL(string: link){ 
    UIApplication.shared.open(url, options: [:], completionHandler: nil) 
} //else you entered an incorrect link 
0

は単にUITableViewDelegate方法didSelectRowAtを実装し、UITableViewの選択された行に対応するためのURLの配列からURLを開くあなたのリンクを開きます。

let urlArray = ["http://google.com", "https://apple.com"] 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     let urlString = self.urlArray[indexPath.row] 
     if let url = URL(string: urlString) 
     { 
      UIApplication.shared.openURL(url) 
     } 
    } 
関連する問題