Uitableview Scroll Erases Data in Text Field Inside UItableviewcell

Textfield data in tableview erasing while scrolling

This is happening bcoz table view cell is reusing(if you dequeued cell, hope you did).

To overcome this, you need to set cell data i.e. textfield data in cellForRowAt:, if user has already entered value init.

You need to track what value is added in which cell textfields so when cell is about to visible cellForRowAt: will be called and you set data in it, so data won't be erased.

UITextField and UITextView disappears after scrolling in tableview?

While reusing the cells it again creates a new memory for the same in some cases. Hence all the widgets and components belongs to the cell obviously will born again. To maintain the previous states of widgets it is required to set every time to the old state for newly created widgets by persisting the old state data.

In your case it requires to persist the text entered in the TextField or TextView through protocol methods from the cell. Below is the brief methodology to achieve the same.

Cell.swift

1.Create a protocol to pass the text to the ViewController like:

 protocol CellDelegate {
func textFieldText(cell: UITableViewCell, text: String)
}

2.Trigger the delegate method every time when text is getting updated. If you call the method in textFieldShouldEndEditing it misses out one case like, Suppose you entered the text with out tap return button in the keyboard if you are available for save button and if tap on it that time the event will not occurred so the actual text is not updating in the persistent model. So it is better to call the delegate every time text is being changed, shouldChangeCharactersInRange method is opt in this case, implementation will be like:

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string
delegate.textFieldText(self, text: result)
}

ViewController.swift

1.create a local variables in the controller to hold each TextField or TextView Data like:

var textFieldText = ""

2.In cellForRowAt method assign the delegate for the cell and update text like below, So every time the cell being created newly it updates the previously entered text to the textField.

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

var cell......

cell.delegate = self
cell.textField.text = textFieldText

return cell
}

3.In the cell delegate method implementation update the text every time, Here we have an argument cell which is useful for determine the current cell by knowing the indexPath.

func textFieldText(cell: UITableViewCell, text: String) {
let indexPath = tableView.indexPathForCell(cell)
textFieldText = text
}

UItextField data disappears when I scroll through the TableView -- Swift

When you scroll back up tableView(tableView: cellForRowAt:) gets called again. Inside that method you increment count every time that is called, thus instead of using the first condition it goes to the second conditional statement that sets cell.TextField.text = "" as count is probably greater than arrayOfNames.count. What are you using count for? Maybe rethink how you could code that part a little better.

text field become Empty when table View Scrolled Swift

Create model of your all field you need and set array as blank as follow.

var dict:[String:Any] = [:]
dict["description"] = ""
arrGetEducationList.append(EducationDetailsArrModel(JSON: dict)!)
self.tblEducation.reloadData()

After in you tableview cell set model data like that

    let edu = self.arrGetEducationList[indexPath.row]
cell.txtSchool.text = edu.edu_institute_name

After that create delegate methods for textfield

func textFieldDidEndEditing(_ textField: UITextField) {
let pointInTable = textField.convert(textField.bounds.origin, to: self.tblEducation)
let IndexPath = self.tblEducation.indexPathForRow(at: pointInTable)
if let cell = tblEducation.cellForRow(at: IndexPath!) as? AddEducationCell1 {

if cell.txtSchool == textField{
arrGetEducationList[(IndexPath?.row)!].edu_institute_name = textField.text!
}
}
}

Value of UITextField in Custom Table view cell changes while scrolling up and scrolling down

Think about it. Every time we see row 0, you are saying:

  regCell?.registrationField.text = self.name

But you never change self.name to what the user types in the field. So naturally when we show row 0 again, it reverts to the old self.name.

Why is ScrollToRect not working for a TextField inside a Tableview, inside a ScrollView?

Converting Rect of a Subview's Subview

Carter's answer got me started, but what I needed to do was get the coordinates of the TextField as it related to the top level ScrollView using ConvertRectFromView:

CGRect frame = currentStep.ScrollView.ConvertRectFromView(tf.Frame, tf.Superview);

currentStep.ScrollView.ScrollRectToVisible(frame, true);

This gave the me the position of the TextField inside the scrollview, instead of the coordinates inside the Cell. Scrolling works as expected after this addition.

Supporting Information

Apple documentation: https://developer.apple.com/reference/uikit/uiview/1622498-convertrect?language=objc

A link that helped me understand how to use the method: http://www.infragistics.com/community/blogs/torrey-betts/archive/2013/05/06/quick-tip-convert-coordinate-system-of-a-uiview.aspx

Objective C usage:

- (CGRect)convertRect:(CGRect)rect 
fromView:(UIView *)view;


Related Topics



Leave a reply



Submit