2016-12-15 1 views
0

Ich habe eine Schaltfläche, die im View Table Controller in xcode 3 verwendet wird und ich möchte alle getbutton auf der rechten Seite, um verschiedene Links zu öffnen, wie kann ich dies tun.Wie fügt man verschiedene Links zu einem Array von Schaltflächen hinzu?

enter image description here

Ich habe keine Ahnung, wie dies zu tun. Ich habe versucht, ein Array hinzuzufügen und auf sie zuzugreifen, aber es hat nicht funktioniert.

+0

Ist das eine benutzerdefinierte UITableViewCell Klasse? Haben Sie nach einem Protokoll für Ihre Zellklasse gesucht und den View-Controller an dieses Protokoll angepasst? Wenn Sie also auf die Schaltfläche tippen, übergeben Sie die Zelle selbst an die Protokollmethode, um ihren Index in der Implementierung abzurufen und den korrekten Index für das von Ihnen erwähnte Array abzurufen. – Prientus

+0

Wie mache ich das? – BallisticDiamond

+0

Ich schicke dir ein Bild von meinem Code, damit du sehen kannst, was ich habe – BallisticDiamond

Antwort

0

Sie müssen das Tag der Schaltfläche in der Methode cellForRow dataSource auf indexPath.row setzen.

cell.button.tag = indexPath.row 

dann in der Tastenauswahlfunktion erhalten Sie die Zelle in dieser Zeile.

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

Dies ist eine Art, wie ich diesen Ansatz würde:

//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

Sie sollten diesen Code schreiben:

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) 
} 
Verwandte Themen