gpt4 book ai didi

ios - 如何为正在编辑的内容提供自定义 UITableViewCell 上下文?

转载 作者:搜寻专家 更新时间:2023-11-01 06:39:14 24 4
gpt4 key购买 nike

现在我有一个自定义 UITableViewCell,其中包含一个 UITextFieldUITextField 在单元格启用时未启用用户交互。当您点击单元格时,textField 将启用用户交互,然后成为第一响应者。 UITextFieldDelegate 在自定义 UITableViewCell 类中实现。我的 View Controller 有一个待办事项存储区,我希望自定义 UITableViewCell 知道它正在编辑哪个待办事项,然后在调用委托(delegate)方法时更新模型和 View 。我该如何实现?

最佳答案

在您的 UITableViewCell 的类文件中,您可以声明如下协议(protocol):

protocol TextFieldUpdateDelegate {
func textFieldHasChanged(textField: UITextField)
}

然后给你 UITableViewCell 类一个实例变量:

var textFieldUpdateDelegate: TextFieldUpdateDelegate?

当您处理 UITextFieldDelegate 方法时,您可能需要像这样回调委托(delegate):

    if (textFieldUpdateDelegate != nil) {
textFieldUpdateDelegate!.textFieldHasChanged(textField)
}

你的 TableView Controller 需要做一些事情:

// declare that it implements the protocol
class YourTableViewController: UITableViewController: TextFieldUpdateDelegate


// Actually implement the method
func textFieldHasChanged(textField: UITextField) {
// Do your magic
}

// Make sure to set the delegate on your table view cells
let cell = self.tableView.dequeueReusableCellWithIdentifier("YourCell") as! YourTableViewCell
cell.textFieldUpdateDelegate = self

希望这足以帮助您将各个部分连接起来。

关于ios - 如何为正在编辑的内容提供自定义 UITableViewCell 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37631754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com