1

Once I have 15 cells in the tableView. In the tableViewCell I have a textfield, I firstly enter the data in textfield of the 1st cell, then click on enter, but while scrolling the entered text is displaying in the other textfield. How to solve it?

In 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}

This is my 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)



    }
meggie
  • 5
  • 6

1 Answers1

0

Declare a array which can store table View text .

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

in you tableview data source set a tag for each textField . and set value from array

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

    } 

in textFieldShouldReturn

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

        texttype.resignFirstResponder()
        let index = textField.tag
        arrString[indexPath.row] = textField.text
        return true
    }
Return Zero
  • 424
  • 4
  • 15