如何使用 Swift 在 UITableViewCell 中获取索引文本字段
How to get index uitextfiled on UITableViewCell with Swift
我正在尝试从 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 的文本字段中输入
将标记设置为 UITextField
,如下面的 UITableView
数据源方法
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
}
我正在尝试从 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 的文本字段中输入
将标记设置为 UITextField
,如下面的 UITableView
数据源方法
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
}