繁体   English   中英

Swift 3:从自定义UITableViewCell中的UITextField检索文本

[英]Swift 3: retrieving text from a UITextField in a custom UITableViewCell

我正在尝试从自定义单元格中的UITextField检索文本,并将其值分配给我的视图控制器中的self.email变量。 当我在textFieldDidEndEditing函数中创建断点时,可以看到textField的值self.email按预期分配给self.email ,但是变量self.email@IBAction doneButtonDidPress函数中为nil。

问题:为什么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
    }
}

@IBAction函数:

@IBAction func doneButtonDidPress(_ sender: Any) {
    print(self.email) // returns nil
}

解决方案是将self.tableView.endEditing(true)放在@IBAction func doneButtonDidPress(_:)

在didendediting中,您没有刷新表视图的数据源。 您的tableview数据源将是一个数组。 您需要将文本字段值分配给相应索引处的数组

textFieldDidEndEditing

   var myCell:UITableViewCell

   var indexPath: NSIndexPath

   myCell = (textField.superview?.superview as! UITableViewCell)

   indexPath = table_view.indexPathForRowAtPoint(myCell.center)!

在此索引路径行,将文本字段的值分配给tableview数据源(数组)

   self.tableview_list.objectAtIndex(indexPath.row).setObject(self.email, forKey: "XYZ")

并且还仅在该特定索引处重新加载tableview。

cellForRowAtIndexPath将设置标签分配给您的textfield并设置UITextfieldDelegate

请注意,这是表视图必须具有文本字段Name和RollNo的示例

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)")
}

希望这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM