【问题标题】:Presenting a UIViewController with a button or UIImageview in a UITableViewCell在 UITableViewCell 中显示带有按钮或 UIImageview 的 UIViewController
【发布时间】:2017-08-16 11:18:55
【问题描述】:

当用户单击表格视图单元格中的按钮时,我试图做到这一点,它将他们带到与当前单元格相关的新视图控制器。

【问题讨论】:

标签: swift3 tableview


【解决方案1】:

试试下面的代码,

在 cellForRowAt indexPath 中执行此操作,

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_Identifier", for: indexPath) as! UITableViewCell
    cell.myButton.isUserInteractionEnabled = true
    cell.myButton.tag = indexPath.row
    cell.myButton.addTarget(self, action: #selector(didTapMyButton(_:)), for: UIControlEvents.touchUpInside)
    return cell
            }

这是按钮操作,

func didTapMyButton(_ sender : UIButton) {
    let selectedIndex = sender.tag // You can get index here, so according to the index you can navigate to particular ViewController
    let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "UIViewController_identifier") as! UIViewController
    self.present(nextVC, animated: true, completion: nil)
}

希望对你有帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多