ios - 为什么我不能通过我的委托(delegate)?

标签 ios swift uitableview delegates

如何使用 delegatesdidTap 的值传递给 ReminderViewController?这是我来自 CustomCell 的代码,其中包含 didTap

protocol BEMCheckBoxDelegate {

func checkBox(cell: CustomCell)

}

class CustomCell: UITableViewCell {

var delegate: BEMCheckBoxDelegate?

override func awakeFromNib() {
    super.awakeFromNib()

}

func didTap(_ checkBox: BEMCheckBox) {

    print("Checkbox is \(checkBox.on)")
    self.delegate?.checkBox(cell: self)

}

然后在我的 ReminderViewController 中:

func checkBox(cell: CustomCell) {
    guard let indexPath = self.tableView.indexPath(for: cell) else{
    return
    }
    print("Button tapped on row \(indexPath.row)")

    let task=tasks[indexPath.row]
    task.checkDone="on"
    (UIApplication.shared.delegate as! AppDelegate).saveContext()

}

最佳答案

也许你可以扩展你的协议(protocol)功能:

protocol BEMCheckBoxDelegate {

    func checkBox(cell: CustomCell, isOn: Bool)
}

然后在 Cell 中,您可以这样调用它:

self.delegate?.checkBox(cell: self, isOn: checkBox.on)

并且不要忘记更改 ReminderViewController 中的函数:

func checkBox(cell: CustomCell, isOn: Bool) {
    ...
}

关于ios - 为什么我不能通过我的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025603/

相关文章:

iphone - 如何在 UITableViewCell 的子类中管理旋转

python - 在iOS/Pythonista上运行Scrapy吗?

ios - 在 UITextview 中获取点击的单词

iphone - 是否可以从 UITextField 自定义键盘?

ios - 在 Swift 中将 JSON 解析到 Realm 数据库时如何防止 Realm 覆盖自身?

ios - UITableViewCell accessoryType UITableViewCellAccessoryCheckmark 重复

ios - 创建数组并将元素分配给 UITableViewCell 并取回 labelText 元素时出现问题

ios - 带有重定向的通用链接在 iOS 中不起作用

ios - 具有检查功能的数组超出范围

ios - 你可以在Xcode的模拟器中测试replaykit吗?