2016-12-15 3 views
0

私はxcode 3のビューテーブルコントローラで使用されるボタンを持っています。右側のすべてのgetbuttonが別のリンクを開くにはどうしたらいいですか?ボタンの配列に異なるリンクを追加するにはどうすればよいですか?

enter image description here

私はこれを行うにはどのようには考えています。配列を追加してアクセスしようとしましたが、動作しませんでした。

+0

これは、カスタムUITableViewCellのクラスですか?あなたはあなたの細胞クラスのためのプロトコルを作成し、そのプロトコルにView Controllerを適合させることを検討しましたか?したがって、ボタンをタップすると、セル自体がプロトコルメソッドに渡され、インプリメンテーションでインデックスが取得され、指定した配列の正しいインデックスが取得されます。 – Prientus

+0

どうすればいいですか? – BallisticDiamond

+0

私はあなたに私のコードの写真を送って、私が持っているものを見ることができます – BallisticDiamond

答えて

0

ボタンのタグをcellForRowのdataSourceメソッドのindexPath.rowに設定する必要があります。

ボタンセレクタ機能では、その行にセルが表示されます。

func buttonTapped(_ sender: UIButton) { 
    guard let tappedCell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? CustomCellClass, let url = URL(string: tappedCell.modelObject.urlString) else { return } 
    //assuming the model object you assign to each cell has a link property, you can now access the link for that cell 
    UIApplication.shared.openURL(url) 
} 
0

これは私がこれに近づくだろうかのようなものです:

//This is your custom cell class 
---------------------------------- 
protocol CustomCellProtocol { 
    func didTapGet(cell:UITableViewCell) 
} 

class CustomCell:UITableViewCell { 

    var delegate:CustomCellProtocol? 

    @IBAction func getTapped(id:Any) { 
     delegate?.didTapGet(cell: self) 
    } 
} 
//This is your View controller 
----------------------------------- 
class YourTableViewController:UIViewController, CustomCellProtocol, UITableViewDataSource { 
//Your Code here 
    var tableView:UITableView? 
    let urlArray = ["www.example1.com", "www.example2.com"] 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! CustomCell 

     //cell code here 
     cell.delegate = self 

     return cell 
    } 

    func didTapGet(cell: UITableViewCell) { 
     if let indexPath = tableView?.indexPath(for: cell), let url = URL(string:urlArray[indexPath.row]) { 

      UIApplication.shared.open(url, options: [], completionHandler: nil) 
     } 
    } 
} 
0

あなたはこのコードを記述する必要があります。

var Urlarr = ["http://drivecurrent.com/using-swift-and-avfoundation-to-create-a-custom-camera-view-for-an-ios-app/","https://fabric.io/kits/ios/crashlytics/summary","http://stackoverflow.com/questions/41156065/how-can-i-add-different-links-to-an-array-of-buttons"] 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return Urlarr.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = self.TableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell 
    cell.TblButton.tag = indexPath.row 
    cell.TblButton.addTarget(self,action:#selector(buttonClicked), 
         for:.touchUpInside) 

    return cell 
} 
func buttonClicked(sender:UIButton) 
{ 
    UIApplication.shared.openURL(NSURL(string: Urlarr[sender.tag]) as! URL) 
} 
関連する問題