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

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.

Related

Accessibility focus goes in space in between the cells in a table view

I am using a table view to show my data. There is no separator view in between the cells but when I switch on the voice over, the focus after on the cell, goes in the space between the two cells ans then on another swipe goes to the next cell.
I am not able to figure out what's going wrong.
The tableView is being imported from another framework, where it is working fine.
The separatorStyle for tableView is set to none.
I just figured out the problem.
In the framework where tableView is populated, array.filter{ !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } was missing at one place where the tableView was passed with the array as data.
so, that focus was on cells with empty spaces as data.
Thank you

UITableView trigger when cell is reused

I'm having some issues with reusable cells in a UITableView. I have several types of cells, that I declare in the constructor.
My issue is that I have one particular type of cell that contains a UITextView and I have an issue when I scroll the table, the text within is lost. I need to save this text to the models that accompany the cells and then put the text back when the cell is used again.
How do I know that the cell is being moved away from? I have other types of cells, so I need a way to invoke some code to do the saving part on the scroll of the UITableView.
I hope that makes sense, if more is required, let me know.
Thanks.
Just save the text once it is changed to the model, check if any text is present and use that in tableView(_:cellForRowAt:)
For more help you will have to show us your code.
You can inherit UITextViewDelegate and in the textViewDidEndEditing(_:) check if the text view is edited, then you will be able to store the text in a variable or somewhere else and restore it whenever you are about to show that cell again.
If there is more than one text view, you might want to set an accessibility identifier for each kind, so you will find out which one did end editing.

Getting the data of all TableView Cell which all has TextFields in it (including the hidden views)

I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.

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.

how to get all text from uitextfield in uitableview

i have a table view with a UITextField in each cell.the table has 30 lines。
i want to get all values from 30 UITextfield,But it works fine for 10 visible cells. I cant't access any invisible cells. the text returns null. please help me.I am a chese developer
I cant't access any invisible cells. the text returns null.
That's how it is supposed to work since UITableView optimizes its use of cells and only displayed cells are actually existing at any time. (at least, if you are using dequeueReusableCellWithIdentifier: as you will find in most UITableView tutorials).
You should possibly store the text entered by the user in each table row somewhere (in your model?) and then access it from there.
Alternatively, you could avoid using dequeueReusableCellWithIdentifier: but in that case you should manage the set of cells in the table view on your own. (I am not encouraging your to do this, it's just a possibility).

Resources