我正在尝试为我的母亲编写一个平板电脑跟踪应用程序,但我遇到了一个问题,我正在使用编辑按钮访问静态表格 View ,但它拒绝传递医学对象。
我需要能够在 prepare(for:sender:) 中获取 indexPath,我通常会使用 tableView.indexPathForSelectedRow 然后使用行从数组中拉出正确的行,但因为我使用的是按钮那是不可能的。
我试过使用标签来存储行,我试过这里其他答案中建议的协议(protocol),但没有成功。当我尝试转到编辑屏幕时,该应用程序仍然崩溃。我在下面有我当前的代码,经过太多次迭代无法列出所有代码
@IBAction func editButtonTapped(_sender: UIButton) {
let point = sender.convert(CGPoint.zero to self.tableView)
buttonIndexPath = self.tableView.indexPathForRow(at: point)
preformSegue(withIdentifier: "showDetails", sender: sender)
}
我的prepare(for:sender:)代码如下
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let destination = segue.destination as! MedicineDetailTableViewController
let selectedMedicine = medicines[(buttonIndexPath?.row)!]
destination.medicine = selectedMedicine
}
}
如果我遗漏了一些愚蠢的东西,我深表歉意,但我无能为力。
最佳答案
您可以为具有类似功能的自定义单元格设置委托(delegate)
func buttonTapped(_ sender: UITableViewCell)
然后您可以使用 sender 在 tableView
上使用 indexPath(for:)
获取 IndexPath
并执行 segue。
然后你的 tableView(_:cellForRowAt:)
看起来像这样:
tableView(_:cellForRowAt:){
//setup other stuff and dequeue cell
cell.delegate = self
return cell
}
关于ios - 使用自定义单元格中的按钮并传递对象执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50394046/