大佬教程收集整理的这篇文章主要介绍了ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经陷入这个问题一段时间了,我真的很赞赏你的建议.这是代码.@H_801_9@
ViewController.swift@H_801_9@
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDatasource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.datasource = self tableView.register(UINib(nibName: "Cell",bundle: nil),forCellReusEIDentifier: "cell") } func tableView(_ tableView: UITableView,numberOfRowsInSection section: int) -> Int { return 3 } func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell return cell } }
import UIKit class Cell: UITableViewCell { @IBOutlet weak var button: UIButton! @IBACtion func buttonPressed(_ sender: Any) { ***[Code to send the pressed cell's number to ViewController]*** } }@H_450_2@
您可以从表视图中找到单元索引路径. (我假设按单元格编号表示索引路径)@H_801_9@
protocol CellDelegate: class { func didTap(_ cell: Cell) } class Cell: UITableViewCell { weak var delegate: CellDelegate? @IBACtion func buttonPressed(_ sender: Any) { delegate?.didTap(self) } } class ViewController: UIViewController,CellDelegate { func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = ... cell.delegate = self return cell } func didTap(_ cell: Cell) { let indexPath = self.tableView.indexPath(for: cell) // do something with the index path } }@H_450_2@ @H_450_2@
以上是大佬教程为你收集整理的ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?全部内容,希望文章能够帮你解决ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。