Ios Swift 3:从自定义UITableViewCell中的UITextField检索文本
我正在尝试从自定义单元格中的Ios Swift 3:从自定义UITableViewCell中的UITextField检索文本,ios,swift,uikit,Ios,Swift,Uikit,我正在尝试从自定义单元格中的UITextField中检索文本,并将值分配给我的视图控制器中的self.email变量。当我在textfielddendediting函数中设置断点时,我可以看到textField的值按预期分配给self.email,但变量self.email在@IBAction donebuttonddpress函数中为零 问题:为什么将self.email的值更改为nil extension ViewController: UITextFieldDelegate { func
UITextField
中检索文本,并将值分配给我的视图控制器中的self.email
变量。当我在textfielddendediting
函数中设置断点时,我可以看到textField
的值按预期分配给self.email
,但变量self.email
在@IBAction donebuttonddpress
函数中为零
问题:为什么将self.email
的值更改为nil
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if let text = textField.text {
self.email = text
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let cell = self.tableView.cellForRow(at: IndexPath(row: 2, section: 0)) as! AboutCell
cell.textView?.becomeFirstResponder()
return true
}
}
我使用的是UITableViewController
,这就是我创建单元格的方式:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "profilePhotoCell")! as! ProfilePhotoCell
cell.delegate = self
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell")! as! TextFieldCell
cell.textField.delegate = self
cell.textLabel?.text = "Email"
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "aboutCell")! as! AboutCell
cell.textView.delegate = self
return cell
}
}
@iAction
功能:
@IBAction func doneButtonDidPress(_ sender: Any) {
print(self.email) // returns nil
}
您没有在DiEndediting中刷新tableview的数据源。tableview数据源将是一个数组。您需要将textfield值分配给相应索引处的数组 在
textfielddidediting
中
var myCell:UITableViewCell
var indexPath: NSIndexPath
myCell = (textField.superview?.superview as! UITableViewCell)
indexPath = table_view.indexPathForRowAtPoint(myCell.center)!
在此索引路径行,将textfield的值分配给tableview数据源(数组)
并仅在该特定索引处重新加载tableview
在cellforrowatinedexpath
中,将set标记分配给您textfield
,并设置UITextfieldDelegate
注意这是tableview必须输入文本字段名和RollNo的一个示例
希望这可能会对您有所帮助。解决方案是将
self.tableView.endEditing(true)
放在@IBAction func donebuttondipress(:)
您是如何声明属性email
(self.email)?@YunCHENvar email=“”
在ViewController
中,你真的在编辑单元格的textField
?@ZonilyJame是的,我是。在行self.email=text
处放置一个断点,然后查看是否再次将其设置为nil
值(或任何分配self.email
值的地方。我刚刚复制了您的代码并进行了测试,它工作正常幸运的是,情况并非如此,因为我只有一个单元格带有textfield。为cell.textfield.tag=1
设置标记,然后在textfielddendediting
中更改,如果textfield.tag=1{self.email=textField.text}我尝试了这些标签。我找到了解决方案,我将其作为解决方案发布。
self.tableview_list.objectAtIndex(indexPath.row).setObject(self.email, forKey: "XYZ")
var txtName:String = ""
var txtRollNo:String = ""
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let YourCell = tableView.dequeueReusableCellWithIdentifier("YourTableViewCellIdentifier") as! YourTableViewCell
YourCell.txtField?.text = itemsData as? String
YourCell.txtField.delegate = self
YourCell.txtField.tag = (indexPath.section*10)+indexPath.row
if(indexPath.section == 0){
switch indexPath.row {
case 0:
YourCell.txtField.placeholder = "Enter Name "
if (self.txtName != ""){
YourCell.txtField.text = self.txtName
}
break;
case 1:
YourCell.txtField.placeholder = "Enter Rollno "
if (self.txtRollNo != ""){
YourCell.txtField.text = self.txtRollNo
}
break;
}
}
return YourCell
}
func textFieldDidBeginEditing(textField: UITextField) {
print(textField.tag)
if textField.tag == 0 {
self.txtName = textField.text
}else if textField.tag == 1{
self.txtRollNo = textField.text
}
}
func textFieldDidEndEditing(textField: UITextField) {
print(textField.tag)
if textField.tag == 0 {
self.txtName = textField.text
}else if textField.tag == 1{
self.txtRollNo = textField.text
}
}
func printValue(){
print("\(self.txtName)")
print("\(self.txtRollNo)")
}