gpt4 book ai didi

ios - 从自定义 tableView 单元格文本字段中获取数据

转载 作者:可可西里 更新时间:2023-11-01 01:09:03 26 4
gpt4 key购买 nike

我有一个 tableView,tableView 内的自定义表格 View 单元格,以及自定义表格 View 单元格内的一些文本字段。

我根据表格 View 单元格中的文本字段中的信息更新了一个类。

我能够使用 didDeselectRowAt 函数成功获取单元格文本字段中的数据以更新我的类,但是,这不是正确的实现,因为它需要用户单击并取消选择文本所在的单元格字段在里面,如果在编辑文本字段后更新类会更好。我搜索了 tableViews 的类似功能,但没有找到有效的。

在我的 CustomTableViewCell 类中,我还能够创建一个在编辑文本字段结束时执行的函数,但这是在另一个类中,我不确定如何从该函数填充我的播放器类。

这是 ViewController 中的代码:

public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell

for item in players {
if item.uniqueRowID == indexPath.row {
item.name = cell.textboxName.text!
}
}
}

我想做类似的事情,用自定义表格 View 单元格内的文本字段中的数据填充我的“玩家”类,但我希望它在每个文本字段和这段代码中完成编辑时发生在 customTableViewCell 类中不起作用。

帮助将不胜感激!

最佳答案

如何使用传递给单元格的模型对象?在单元内,可以在用户交互时进行任何更新。对象的编辑触发器保存在单元格中,可以立即生效。

这是一个人为的例子。

final class UIViewController: UITableViewDataSource {

var players: [Player] = [] // Players set by something in the view controller.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

guard let cell = tableView.cellForRow(at: indexPath) as? CustomTableViewCell else { fatalError() }

cell.player = players[indexPath.row]

return cell
}
}

在单元格本身中:

final CustomTableViewCell: UITableViewCell, UITextFieldDelegate {

var player: Player!

weak var textField: UITextField!

func textFieldDidEndEditing(_ textField: UITextField) {

player.name = textField.text
}
}

在此基础上,您可以选择使用 ViewModel 进一步抽象关系。

关于ios - 从自定义 tableView 单元格文本字段中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49752220/

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