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)
}
}
}
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
Wie mache ich das? – BallisticDiamond
Ich schicke dir ein Bild von meinem Code, damit du sehen kannst, was ich habe – BallisticDiamond