Textfield data in tableview erasing while scrolling [duplicate] - ios

This question already has an answer here:
UITableview Scroll erases data in text field inside UITableviewcell
(1 answer)
Closed 4 years ago.
I have taken two custom tableview cells in one tableview.One cell is for textfield and another for dropdown.If I scroll the tableview after filling the data in textfields it was removing(data in textfields is removing).Can anyone please help on this.

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.

Related

Information from a PickerView appears in another line of the TableView. How to solve?

Hello programmers friends. I have a situation I can not explain. I'll leave the prints on the screens and then describe what happened.
-
-
[-][3]
Initially I select the quantity in the pickerview and it changes in the UITextField field. Then when I scroll the screen, the 6 row of the table is with the PickerView information from row 1, and then when I roll the screen up, the data is lost. Has anyone ever had a similar problem?
When a quantity is selected, you need to persist that information in your data model. UITableView only creates enough rows to show what's visible on screen and it re-creates rows as they are scrolled into view. So you need to have the quantity information stored elsewhere to provide to the table view when it asks again for the row information. Hope this makes sense?
As Paulw11 mentioned, cells within a UITableView or a UICollectionView are reused, thus if you are manually creating the UIPickerView within the cell itself you have two options:
1) Rather than create the UIPickerView via code, subclass the UICollectionViewCell being used and add the UIPickerView either in the Xib / Storyboard(where ever you created the cell view).
2) In the prepareForReuse() function within a cell, check if the UIPickerView variable is not nil, and if it is not nil, then remove it and set it to nil.

Accessing information from a cell in a new UItableView (Swift) [duplicate]

This question already has answers here:
Find the indexPath of a button inside UITableViewCell when button pressed?
(6 answers)
Closed 6 years ago.
I have a table view which has cells in it. In each cell there is a button to take you to a new UITableView. I am trying to figure out how to get the index of the cell I clicked the button in from the new UITableView because depending on what cell index I choose it needs to show different items in the new UITableView it takes you to. Any help would be appreciated.
set tag property of the button equal to the indexPath.row in cellForRowAtIndexPath method or in your custom cell class if you are using one.
Hence, when its clicked, you can get the value of indexPath.row from its tag (sender.tag).
Note: Assuming there is only one section in your tableview and you wont be inserting or deleting rows runtime. You will have to reload the table in such scenarios.
The best solution that I personally use is, to pass the cell a model object via property or custom method. And later, use the object to make further decisions.

Updating label in a cell of UITableView

My UITableViewCell has a cell template which is created from another nib file. The cell has a UIlabel object. Now, once the UITableView has loaded and the text has been displayed, and if I want to change its value by clicking a button from another cell, How should I do it ?
I have updated the text of the UIlabel but how to show it on the screen? Should I reload the entire table? Kindly let me know if there is any good way to do it.
You can use KVO for this purpose. Each cell observes the model, and when it changes, update some fields.

UITextField values inside UICollectionView cell in swift

I have UITextField and an ImageView inside UICollectionView cell. It loads data from web service. The user can input data inside the text field and have to submit it. My problem is, when I scroll the collection view, the entered value in one text field in a cell got messed up with other text field values from another cell, and it displays wrong values.
my steps(suppose I have cells A, B, ......., k)
1.entering values
Cell A Textfield= 12
Cell B Textfield= 13
2.scrolling down
(I haven't entered anything in cell F Textfield, though it shows 13)
3.scrolling back to where I entered values
Cell A Textfield= 12
Cell B Textfield= (blank)
You can solve this problem by keeping the values of UITextField in an Array. Whenever you are entering value to UITextField and dismissing keyboard,then save that value to array at the same cell index value in array and when you scroll your collectionView, the textfield value should entered from array and it won't misplace value.
UICollectionView and UITableView reuse a handful of cell objects as you scroll. This helps with memory management. It also means that when you populate one text field on a cell, that text field's cell will get reused for a different row as you scroll, so if you don't reset the text field's content, you'll see the same data showing up for the wrong rows.
The solution is that your "source of truth" (aka model) must always be separate from your UI. So when the user types something in a text field, your model (perhaps an array of strings?) should be updated accordingly. Then when the user scrolls, you must use tableView:cellForRowAtIndexPath: or collectionView:cellForItemAtIndexPath: methods to populate the cell with the appropriate data. If there is no data, you must clear any data that may have been left over from another row whose cell got reused.
Other opportunities you'll have to deal with cell reuse include the willDisplayCellForRowAtIndexPath method on UITableViewDelegate, or prepareForReuse on UITableViewCell. Collection view objects have corresponding methods as well.
As far as I can understand from question I think the problem is, you are not updating values every time - cellForRowAtIndexPath function gets called on scrolling.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
what you need to do to resolve this issue is "You have to store all the values you have entered in your cell's textField and whenever above datasource method gets called for UITableView, updated cell accordingly".

adding a control to a custom cell view

I am getting really frustrated trying to solve this problem i tried implementing it in many many ways but no solution. I have a UIStepper in a custom cell and i want to change a value on the cell. Everything works fine expect when i scroll around the tableView the values from the UIStepper changes from one cell to another. Please help here are my screen shoots.
Link to tableview implementation and link to cell implementation
You are trying to store the stepper value inside each individual cell. That's not going to work because cells are reused; the cell that you now see in row 2 may reappear in row 20 when the user scrolls.
That is why you must store the value for the stepper in the model (your data) on a row-by-row basis, so that you can set it freshly and correctly for that row every single time cellForRowAtIndexPath: is called.
This, in turn, means that as the user steps the stepper, its valueChanged is going to need to talk to the table view data source so that the model can be updated ("the stepper value for row 5 has just been changed to 3") and maintained in the model.

Resources