gpt4 book ai didi

ios - 如何使用 Swift 在 UITableViewCell 中获取索引 uitextfield

转载 作者:行者123 更新时间:2023-11-28 12:11:38 37 4
gpt4 key购买 nike

我正在尝试从 UITableViewCell 索引中检索文本字段值并更新到领域。代码:

 func textFieldDidBeginEditing(_ textField: UITextField) {
let index = IndexPath(row: 0, section: 0)
let cell: OthersTableViewCell = self.tableView.cellForRow(at: index) as! OthersTableViewCell
self.firstnameOther = cell.firstNameTxt.text!
self.lastnameOther = cell.lastNameTxt.text!
self.countId = index.row
print("self.countId\(self.countId)")
try! realm.write {
realm.create(Others.self, value: ["id": self.id, "firstname": self.firstnameOther!, "lastname": self.lastnameOther!], update: true)
}

others = realm.objects(Others.self)
print(others)
}

我需要按索引行在 uitabelview 的文本字段中输入

最佳答案

UITableView 数据源方法中将标签设置为 UITextField

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//Create you UITableViewCell Object and do your stuff then set tag to firstNameTxt by indexPath.row

cell.firstNameTxt.tag = indexPath.row
}

然后像下面这样使用它

func textFieldDidBeginEditing(_ textField: UITextField) {
let index = IndexPath(row: textField.tag, section: 0)
let cell: OthersTableViewCell = self.tableView.cellForRow(at: index) as! OthersTableViewCell

//Do your stuff
}

关于ios - 如何使用 Swift 在 UITableViewCell 中获取索引 uitextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48315530/

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