从其他单元格输入一个数据后如何清除文本字段中的文本

时间:2018-09-22 10:28:41

标签: ios uitableview

一旦tableView中有15个单元格。在tableViewCell中,我有一个textfield,我首先在第一个单元格的textfield中输入数据,然后单击Enter,但是在滚动输入文本的同时在另一个文本字段中显示。如何解决?

在viewController中:-

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


        let identifier = "Cell"
        var cell: QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell

        if cell == nil {
            tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
        }
        cell.contentView.backgroundColor = UIColor.clear






            let questionsModel = questionViewModel.titleForHeaderInSection(atsection:indexPath.section)
            print(questionsModel.buttontype)

            questionViewModel.button = questionsModel.buttontype

            let optionModel = questionViewModel.datafordisplay(atindex: indexPath)
            cell.setOptions(Options1: optionModel)

            print("Section \(indexPath.section), Row : \(indexPath.row)")






        if questionViewModel.button == "5"{


            self.selected = 1
            cell.option5()
        cell.setOtherOptions(Options:questionViewModel.datafordisplay(atindex: indexPath))



        }




            print("text")

            cell.textadd = {[weak self] in

                if let i = self?.tableview.indexPath(for: $0) {


                    print("the selected text\(cell.textname ?? "")")

                 print(i)

                    print("the Selected button is \(cell.textname ?? "")")

                    print(i)

                    cell.setText(Options:(self?.questionViewModel.datafordisplay(atindex: indexPath))!)



                    let model = self?.questionViewModel.datafordisplay(atindex: indexPath)
                    print(model?.values ?? "")

                    print("Section \(indexPath.section), Row : \(indexPath.row)")

                    let indexPath = IndexPath(row: indexPath.row, section:indexPath.section);
                    self?.tableview.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
                    self?.tableView((self?.tableview)!, didSelectRowAt: indexPath)
                    }
                print(cell.texttype.text ?? "")
                self?.u = 1
            }

return cell}

这是我的tableviewcell:-

      @IBOutlet weak var texttype: UITextField!
     var textname:String?

            func textFieldDidBeginEditing(_ textField: UITextField) {
           texttype.text = ""


        }
        func textFieldShouldClear(_ textField: UITextField) -> Bool
        {
                   return true
        }

        func textFieldDidEndEditing(_ textField: UITextField) {
              self.TextAction(texttype)

        }

        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {


            return true;
        }



        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {

            return true;

        }




        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            print(texttype.text)
            texttype.resignFirstResponder()

      //   self.TextAction(texttype)

            return true
        }

     var textadd : ((NH_QuestionListCell) -> Void)?
        @IBAction func TextAction(_ sender: UITextField) {

             print(texttype.text)
                //    self.question.text = texttype.text
            self.textname = texttype.text
            print(self.textname)
                    print(texttype.text)
                 textadd?(self)
                }


 func setText(Options:NH_OptionsModel)


    {
        self.displaySmiley.isHidden = true
        self.viewdisplay.isHidden = false
        self.imagebutton.isHidden = true
        self.question.isHidden = true
        self.displayRatingView.isHidden = true

        print(Options.values)
        Options.values = self.textname
        print(Options.values)



    }

1 个答案:

答案 0 :(得分:0)

声明一个可以存储表View文本的数组。

var arrString = ["","",""] // each empty string for each text field 

在tableview数据源中,为每个textField设置一个标签。并从数组中设置值

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
        cell.textField.tag = indexPath.row
        cell.textField.text = arrString[indexPath.row]
        return cell

    } 

在textFieldShouldReturn

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        texttype.resignFirstResponder()
        let index = textField.tag
        arrString[indexPath.row] = textField.text
        return true
    }